Skip to content

Commit 2e77db1

Browse files
acmelgithub-actions[bot]
authored andcommitted
perf hist stdio: Do bounds check when printing callchains to avoid UB with new gcc versions
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> (cherry picked from commit 2333cfa) Signed-off-by: Andrea Righi <arighi@nvidia.com>
1 parent 31346d3 commit 2e77db1

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

tools/perf/ui/stdio/hist.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#include <limits.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <linux/string.h>
@@ -24,6 +25,9 @@ static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
2425
int i;
2526
int ret = fprintf(fp, " ");
2627

28+
if (left_margin > USHRT_MAX)
29+
left_margin = USHRT_MAX;
30+
2731
for (i = 0; i < left_margin; i++)
2832
ret += fprintf(fp, " ");
2933

0 commit comments

Comments
 (0)