Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf test: Fix variable length array undefined behavior in bp_account

Fix:

tests/bp_account.c:154:9: runtime error: variable length array bound evaluates to non-positive value 0

by switching from a variable length to an allocated array.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20220610180247.444798-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
cc214552 94725994

+14 -2
+14 -2
tools/perf/tests/bp_account.c
··· 151 151 static int detect_share(int wp_cnt, int bp_cnt) 152 152 { 153 153 struct perf_event_attr attr; 154 - int i, fd[wp_cnt + bp_cnt], ret; 154 + int i, *fd = NULL, ret = -1; 155 + 156 + if (wp_cnt + bp_cnt == 0) 157 + return 0; 158 + 159 + fd = malloc(sizeof(int) * (wp_cnt + bp_cnt)); 160 + if (!fd) 161 + return -1; 155 162 156 163 for (i = 0; i < wp_cnt; i++) { 157 164 fd[i] = wp_event((void *)&the_var, &attr); 158 - TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1); 165 + if (fd[i] == -1) { 166 + pr_err("failed to create wp\n"); 167 + goto out; 168 + } 159 169 } 160 170 161 171 for (; i < (bp_cnt + wp_cnt); i++) { ··· 176 166 177 167 ret = i != (bp_cnt + wp_cnt); 178 168 169 + out: 179 170 while (i--) 180 171 close(fd[i]); 181 172 173 + free(fd); 182 174 return ret; 183 175 } 184 176