Skip to contentSkip to conversation

libperf: Use struct_size() helper

PATCH New Gustavo A. R. Silva 1 participant 1 message Jun 22, 2020 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libperf: Use struct_size() helper

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. Also, remove unnecessary variable _size_.

This code was detected with the help of Coccinelle and, audited and fixed manually.

---
 tools/lib/perf/threadmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/perf/threadmap.c +2 -2
index e92c368b0a6c..86d9dabd7fe5 100644
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <asm/bug.h>
 #include <stdio.h>
+#include <linux/overflow.h>
 
 static void perf_thread_map__reset(struct perf_thread_map *map, int start, int nr)
 {
@@ -17,10 +18,9 @@ static void perf_thread_map__reset(struct perf_thread_map *map, int start, int n
 
 struct perf_thread_map *perf_thread_map__realloc(struct perf_thread_map *map, int nr)
 {
-	size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
 	int start = map ? map->nr : 0;
 
-	map = realloc(map, size);
+	map = realloc(map, struct_size(map, map, nr));
 	/*
 	 * We only realloc to add more items, let's reset new items.
 	 */
Signature · 1 line
2.27.0