Skip to contentSkip to conversation

Use flexible_array_size() helper in memcpy()

PATCH Applied Gustavo A. R. Silva 2 participants 5 messages Jul 31, 2020 – Aug 4, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index e84c2b5596f2..bd7f972ceea4 100644
@@ -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
#3 lore raw

PATCH 2/2 mm: memcontrol: Use the preferred form for passing the size of a structure type

In reply to 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.

---
 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memcontrol.c +1 -1
index bd7f972ceea4..bd0f56785841 100644
@@ -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
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy()

In reply to Gustavo A. R. Silva
Quoted 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
Quoted 24 lines
---
 mm/memcontrol.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
mm/memcontrol.c +3 -4
index e84c2b5596f2..bd7f972ceea4 100644
@@ -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
Acked-by on v1 2/2
#4 lore raw

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. Silva
Quoted 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.

Quoted 19 lines
---
 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memcontrol.c +1 -1
index bd7f972ceea4..bd0f56785841 100644
@@ -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