All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] perf annotate: Use struct_size() helper
@ 2020-06-23  0:05 Gustavo A. R. Silva
  0 siblings, 0 replies; only message in thread
From: Gustavo A. R. Silva @ 2020-06-23  0:05 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-kernel, Gustavo A. R. Silva

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

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

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 tools/perf/util/annotate.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 76bfb4a9d94e..9330520c34a0 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -43,6 +43,7 @@
 #include <linux/string.h>
 #include <subcmd/parse-options.h>
 #include <subcmd/run-command.h>
+#include <linux/overflow.h>
 
 /* FIXME: For the HE_COLORSET */
 #include "ui/browser.h"
@@ -775,7 +776,7 @@ static int annotated_source__alloc_histograms(struct annotated_source *src,
 	if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
 		return -1;
 
-	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
+	sizeof_sym_hist = struct_size(src->histograms, addr, size);
 
 	/* Check for overflow in zalloc argument */
 	if (sizeof_sym_hist > SIZE_MAX / nr_hists)
@@ -1167,13 +1168,6 @@ static void annotation_line__exit(struct annotation_line *al)
 	zfree(&al->line);
 }
 
-static size_t disasm_line_size(int nr)
-{
-	struct annotation_line *al;
-
-	return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
-}
-
 /*
  * Allocating the disasm annotation line data with
  * following structure:
@@ -1193,7 +1187,7 @@ static struct disasm_line *disasm_line__new(struct annotate_args *args)
 	if (evsel__is_group_event(args->evsel))
 		nr = args->evsel->core.nr_members;
 
-	dl = zalloc(disasm_line_size(nr));
+	dl = zalloc(struct_size(dl, al.data, nr));
 	if (!dl)
 		return NULL;
 
-- 
2.27.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-06-22 23:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23  0:05 [PATCH][next] perf annotate: Use struct_size() helper Gustavo A. R. Silva

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.