Skip to content

Commit 03bfcda

Browse files
guidosarduccianakryiko
authored andcommitted
selftests/bpf: Fix arg parsing in veristat, test_progs
Current code parses arguments with strtok_r() using a construct like char *state = NULL; while ((next = strtok_r(state ? NULL : input, ",", &state))) { ... } where logic assumes the 'state' var can distinguish between first and subsequent strtok_r() calls, and adjusts parameters accordingly. However, 'state' is strictly internal context for strtok_r() and no such assumptions are supported in the man page. Moreover, the exact behaviour of 'state' depends on the libc implementation, making the above code fragile. Indeed, invoking "./test_progs -t <test_name>" on mips64el/musl will hang, with the above code in an infinite loop. Similarly, we see strange behaviour running 'veristat' on mips64el/musl: $ ./veristat -e file,prog,verdict,insns -C two-ok add-failure Can't specify more than 9 stats Rewrite code using a counter to distinguish between strtok_r() calls. Fixes: 61ddff3 ("selftests/bpf: Improve by-name subtest selection logic in prog_tests") Fixes: 394169b ("selftests/bpf: add comparison mode to veristat") Fixes: c8bc5e0 ("selftests/bpf: Add veristat tool for mass-verifying BPF object files") Signed-off-by: Tony Ambardar <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/392d8bf5559f85fa37926c1494e62312ef252c3d.1722244708.git.tony.ambardar@gmail.com
1 parent c024780 commit 03bfcda

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

tools/testing/selftests/bpf/testing_helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ int parse_test_list(const char *s,
221221
bool is_glob_pattern)
222222
{
223223
char *input, *state = NULL, *test_spec;
224-
int err = 0;
224+
int err = 0, cnt = 0;
225225

226226
input = strdup(s);
227227
if (!input)
228228
return -ENOMEM;
229229

230-
while ((test_spec = strtok_r(state ? NULL : input, ",", &state))) {
230+
while ((test_spec = strtok_r(cnt++ ? NULL : input, ",", &state))) {
231231
err = insert_test(set, test_spec, is_glob_pattern);
232232
if (err)
233233
break;

tools/testing/selftests/bpf/veristat.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,13 @@ static int parse_stat(const char *stat_name, struct stat_specs *specs)
785785
static int parse_stats(const char *stats_str, struct stat_specs *specs)
786786
{
787787
char *input, *state = NULL, *next;
788-
int err;
788+
int err, cnt = 0;
789789

790790
input = strdup(stats_str);
791791
if (!input)
792792
return -ENOMEM;
793793

794-
while ((next = strtok_r(state ? NULL : input, ",", &state))) {
794+
while ((next = strtok_r(cnt++ ? NULL : input, ",", &state))) {
795795
err = parse_stat(next, specs);
796796
if (err) {
797797
free(input);
@@ -1495,7 +1495,7 @@ static int parse_stats_csv(const char *filename, struct stat_specs *specs,
14951495
while (fgets(line, sizeof(line), f)) {
14961496
char *input = line, *state = NULL, *next;
14971497
struct verif_stats *st = NULL;
1498-
int col = 0;
1498+
int col = 0, cnt = 0;
14991499

15001500
if (!header) {
15011501
void *tmp;
@@ -1513,7 +1513,7 @@ static int parse_stats_csv(const char *filename, struct stat_specs *specs,
15131513
*stat_cntp += 1;
15141514
}
15151515

1516-
while ((next = strtok_r(state ? NULL : input, ",\n", &state))) {
1516+
while ((next = strtok_r(cnt++ ? NULL : input, ",\n", &state))) {
15171517
if (header) {
15181518
/* for the first line, set up spec stats */
15191519
err = parse_stat(next, specs);

0 commit comments

Comments
 (0)