History

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.

Ian Rogers committed 4 years ago

Commit cc214551 parent 94725991 file · +14 −2Browse files

Unified · Split

tools/perf/tests/bp_account.c+14−2
@@ -151,11 +151,21 @@ static int detect_ioctl(void) · 150 unmodified lines
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) · 4 unmodified lines
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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t