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
151static int detect_share(int wp_cnt, int bp_cnt)151static int detect_share(int wp_cnt, int bp_cnt)
152{152{
153 struct perf_event_attr attr;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 for (i = 0; i < wp_cnt; i++) {163 for (i = 0; i < wp_cnt; i++) {
157 fd[i] = wp_event((void *)&the_var, &attr);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 for (; i < (bp_cnt + wp_cnt); i++) {171 for (; i < (bp_cnt + wp_cnt); i++) {
@@ -166,9 +176,11 @@ static int detect_share(int wp_cnt, int bp_cnt) · 4 unmodified lines
166 176
167 ret = i != (bp_cnt + wp_cnt);177 ret = i != (bp_cnt + wp_cnt);
168 178
179out:
169 while (i--)180 while (i--)
170 close(fd[i]);181 close(fd[i]);
171 182
183 free(fd);
172 return ret;184 return ret;
173}185}
174 186

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