Use flexible_array_size() helper in memcpy()
Subsystems (3)
cover Use flexible_array_size() helper in memcpy()
Hi,
This small series aims to:
1. Make use of the flexible_array_size() helper in a call to memcpy()
2. While there, use the preferred form for passing the size of a structure type
Thanks
Gustavo A. R. Silva (2):
mm: memcontrol: Use flex_array_size() helper in memcpy()
mm: memcontrol: Use the preferred form for passing the size of a
structure type
mm/memcontrol.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/2 mm: memcontrol: Use flex_array_size() helper in memcpy()
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.
--- mm/memcontrol.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
mm/memcontrol.c +3 -4
@@ -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;
Signature · 1 line
2.27.0
PATCH 2/2 mm: memcontrol: Use the preferred form for passing the size of a structure type
In reply to Gustavo A. R. SilvaUse the preferred form for passing the size of a structure type. The alternative form where the structure type is spelled out hurts readability and introduces an opportunity for a bug when the object type is changed but the corresponding object identifier to which the sizeof operator is applied is not.
--- mm/memcontrol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memcontrol.c +1 -1
@@ -4255,7 +4255,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, new->entries[size -1 ].threshold = threshold;/* Sort thresholds. Registering of new threshold isn't time-critical */ -sort (new->entries, size,sizeof (struct mem_cgroup_threshold ), +sort (new->entries, size,sizeof (*new->entries), compare_thresholds,NULL );/* Find current threshold */
Signature · 1 line
2.27.0
Re: [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy()
In reply to Gustavo A. R. SilvaQuoted 8 lines · Gustavo A. R. Silva
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.
Quoted 1 line
Signed-off-by Gustavo A. R. Silva <[email protected]>
Quoted 24 lines
--- mm/memcontrol.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)mm/memcontrol.c +3
-4@@ -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;Signature · 1 line
2.27.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type
In reply to Gustavo A. R. SilvaQuoted 7 lines · Gustavo A. R. Silva
Use the preferred form for passing the size of a structure type. The alternative form where the structure type is spelled out hurts readability and introduces an opportunity for a bug when the object type is changed but the corresponding object identifier to which the sizeof operator is applied is not.
Signed-off-by Gustavo A. R. Silva <[email protected]>
Quoted 19 lines
--- mm/memcontrol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/memcontrol.c +1
-1@@ -4255,7 +4255,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, new->entries[size -1 ].threshold = threshold;/* Sort thresholds. Registering of new threshold isn't time-critical */ -sort (new->entries, size,sizeof (struct mem_cgroup_threshold ), +sort (new->entries, size,sizeof (*new->entries), compare_thresholds,NULL );/* Find current threshold */ Signature · 1 line
2.27.0
Signature · 2 lines
Michal Hocko SUSE Labs