[1/2,next] mm: memcontrol: Use flex_array_size() helper in memcpy()
diff mbox series

Message ID ddd60dae2d9aea1ccdd2be66634815c93696125e.1596214831.git.gustavoars@kernel.org
State New
Headers show
Series
  • Use flexible_array_size() helper in memcpy()
Related show

Commit Message

Gustavo A. R. Silva July 31, 2020, 5:12 p.m. UTC
Make use of the flex_array_size() helper to calculate the size of a
flexible array member within an enclosing structure.

This helper offers defense-in-depth against potential integer
overflows, while at the same time makes it explicitly clear that
we are dealing with a flexible array member.

Also, remove unnecessary braces.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 mm/memcontrol.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Michal Hocko Aug. 4, 2020, 1:25 p.m. UTC | #1
On Fri 31-07-20 12:12:53, Gustavo A. R. Silva wrote:
> Make use of the flex_array_size() helper to calculate the size of a
> flexible array member within an enclosing structure.
> 
> This helper offers defense-in-depth against potential integer
> overflows, while at the same time makes it explicitly clear that
> we are dealing with a flexible array member.
> 
> Also, remove unnecessary braces.

I was not aware of this helper. The code looks slightly better.
 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Acked-by: Michal Hocko <mhocko@suse.com>
> ---
>  mm/memcontrol.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index e84c2b5596f2..bd7f972ceea4 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4246,10 +4246,9 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
>  	new->size = size;
>  
>  	/* Copy thresholds (if any) to new array */
> -	if (thresholds->primary) {
> -		memcpy(new->entries, thresholds->primary->entries, (size - 1) *
> -				sizeof(struct mem_cgroup_threshold));
> -	}
> +	if (thresholds->primary)
> +		memcpy(new->entries, thresholds->primary->entries,
> +		       flex_array_size(new, entries, size - 1));
>  
>  	/* Add new threshold */
>  	new->entries[size - 1].eventfd = eventfd;
> -- 
> 2.27.0

Patch
diff mbox series

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e84c2b5596f2..bd7f972ceea4 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4246,10 +4246,9 @@  static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
 	new->size = size;
 
 	/* Copy thresholds (if any) to new array */
-	if (thresholds->primary) {
-		memcpy(new->entries, thresholds->primary->entries, (size - 1) *
-				sizeof(struct mem_cgroup_threshold));
-	}
+	if (thresholds->primary)
+		memcpy(new->entries, thresholds->primary->entries,
+		       flex_array_size(new, entries, size - 1));
 
 	/* Add new threshold */
 	new->entries[size - 1].eventfd = eventfd;