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 <gustavo...@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