]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
perf hist stdio: Do bounds check when printing callchains to avoid UB with new gcc...
authorArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 10 Mar 2025 19:45:33 +0000 (16:45 -0300)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 13 Mar 2025 07:30:14 +0000 (00:30 -0700)
Do a simple bounds check to avoid this on new gcc versions:

  31    15.81 fedora:rawhide                : FAIL gcc version 15.0.1 20250225 (Red Hat 15.0.1-0) (GCC)
    In function 'callchain__fprintf_left_margin',
        inlined from 'callchain__fprintf_graph.constprop' at ui/stdio/hist.c:246:12:
    ui/stdio/hist.c:27:39: error: iteration 2147483647 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
       27 |         for (i = 0; i < left_margin; i++)
          |                                      ~^~
    ui/stdio/hist.c:27:23: note: within this loop
       27 |         for (i = 0; i < left_margin; i++)
          |                     ~~^~~~~~~~~~~~~
    cc1: all warnings being treated as errors

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Link: https://lore.kernel.org/r/20250310194534.265487-4-acme@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/ui/stdio/hist.c

index 74b2c619c56c8ba330bb092c424b359e7a0a6c4e..7ac4b98e28bca82ed55b04f7d01800d6d942312d 100644 (file)
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <linux/string.h>
@@ -24,6 +25,9 @@ static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
        int i;
        int ret = fprintf(fp, "            ");
 
+       if (left_margin > USHRT_MAX)
+               left_margin = USHRT_MAX;
+
        for (i = 0; i < left_margin; i++)
                ret += fprintf(fp, " ");