Skip to content

Commit cc21455

Browse files
captain5050acmel
authored andcommitted
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>
1 parent 9472599 commit cc21455

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

tools/perf/tests/bp_account.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,21 @@ static int detect_ioctl(void)
151151
static int detect_share(int wp_cnt, int bp_cnt)
152152
{
153153
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;
155162

156163
for (i = 0; i < wp_cnt; i++) {
157164
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+
}
159169
}
160170

161171
for (; i < (bp_cnt + wp_cnt); i++) {
@@ -166,9 +176,11 @@ static int detect_share(int wp_cnt, int bp_cnt)
166176

167177
ret = i != (bp_cnt + wp_cnt);
168178

179+
out:
169180
while (i--)
170181
close(fd[i]);
171182

183+
free(fd);
172184
return ret;
173185
}
174186

0 commit comments

Comments
 (0)