Skip to content

Commit 929e54a

Browse files
Jianlin-lvAlexei Starovoitov
authored andcommitted
bpf: Fix compilation warning of selftests
Clang compiler version: 12.0.0 The following warning appears during the selftests/bpf compilation: prog_tests/send_signal.c:51:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result] 51 | write(pipe_c2p[1], buf, 1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ prog_tests/send_signal.c:54:3: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result] 54 | read(pipe_p2c[0], buf, 1); | ^~~~~~~~~~~~~~~~~~~~~~~~~ ...... prog_tests/stacktrace_build_id_nmi.c:13:2: warning: ignoring return value of ‘fscanf’,declared with attribute warn_unused_result [-Wunused-resul] 13 | fscanf(f, "%llu", &sample_freq); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test_tcpnotify_user.c:133:2: warning:ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result] 133 | system(test_script); | ^~~~~~~~~~~~~~~~~~~ test_tcpnotify_user.c:138:2: warning:ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result] 138 | system(test_script); | ^~~~~~~~~~~~~~~~~~~ test_tcpnotify_user.c:143:2: warning:ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result] 143 | system(test_script); | ^~~~~~~~~~~~~~~~~~~ Add code that fix compilation warning about ignoring return value and handles any errors; Check return value of library`s API make the code more secure. Signed-off-by: Jianlin Lv <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6fc5916 commit 929e54a

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

tools/testing/selftests/bpf/prog_tests/send_signal.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,19 @@ static void test_send_signal_common(struct perf_event_attr *attr,
4848
close(pipe_p2c[1]); /* close write */
4949

5050
/* notify parent signal handler is installed */
51-
write(pipe_c2p[1], buf, 1);
51+
CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
5252

5353
/* make sure parent enabled bpf program to send_signal */
54-
read(pipe_p2c[0], buf, 1);
54+
CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
5555

5656
/* wait a little for signal handler */
5757
sleep(1);
5858

59-
if (sigusr1_received)
60-
write(pipe_c2p[1], "2", 1);
61-
else
62-
write(pipe_c2p[1], "0", 1);
59+
buf[0] = sigusr1_received ? '2' : '0';
60+
CHECK(write(pipe_c2p[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
6361

6462
/* wait for parent notification and exit */
65-
read(pipe_p2c[0], buf, 1);
63+
CHECK(read(pipe_p2c[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
6664

6765
close(pipe_c2p[1]);
6866
close(pipe_p2c[0]);
@@ -99,15 +97,15 @@ static void test_send_signal_common(struct perf_event_attr *attr,
9997
}
10098

10199
/* wait until child signal handler installed */
102-
read(pipe_c2p[0], buf, 1);
100+
CHECK(read(pipe_c2p[0], buf, 1) != 1, "pipe_read", "err %d\n", -errno);
103101

104102
/* trigger the bpf send_signal */
105103
skel->bss->pid = pid;
106104
skel->bss->sig = SIGUSR1;
107105
skel->bss->signal_thread = signal_thread;
108106

109107
/* notify child that bpf program can send_signal now */
110-
write(pipe_p2c[1], buf, 1);
108+
CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
111109

112110
/* wait for result */
113111
err = read(pipe_c2p[0], buf, 1);
@@ -121,7 +119,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
121119
CHECK(buf[0] != '2', test_name, "incorrect result\n");
122120

123121
/* notify child safe to exit */
124-
write(pipe_p2c[1], buf, 1);
122+
CHECK(write(pipe_p2c[1], buf, 1) != 1, "pipe_write", "err %d\n", -errno);
125123

126124
disable_pmu:
127125
close(pmu_fd);

tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ static __u64 read_perf_max_sample_freq(void)
66
{
77
__u64 sample_freq = 5000; /* fallback to 5000 on error */
88
FILE *f;
9+
__u32 duration = 0;
910

1011
f = fopen("/proc/sys/kernel/perf_event_max_sample_rate", "r");
1112
if (f == NULL)
1213
return sample_freq;
13-
fscanf(f, "%llu", &sample_freq);
14+
CHECK(fscanf(f, "%llu", &sample_freq) != 1, "Get max sample rate",
15+
"return default value: 5000,err %d\n", -errno);
1416
fclose(f);
1517
return sample_freq;
1618
}

tools/testing/selftests/bpf/test_tcpnotify_user.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,24 @@ int main(int argc, char **argv)
124124
sprintf(test_script,
125125
"iptables -A INPUT -p tcp --dport %d -j DROP",
126126
TESTPORT);
127-
system(test_script);
127+
if (system(test_script)) {
128+
printf("FAILED: execute command: %s, err %d\n", test_script, -errno);
129+
goto err;
130+
}
128131

129132
sprintf(test_script,
130133
"nc 127.0.0.1 %d < /etc/passwd > /dev/null 2>&1 ",
131134
TESTPORT);
132-
system(test_script);
135+
if (system(test_script))
136+
printf("execute command: %s, err %d\n", test_script, -errno);
133137

134138
sprintf(test_script,
135139
"iptables -D INPUT -p tcp --dport %d -j DROP",
136140
TESTPORT);
137-
system(test_script);
141+
if (system(test_script)) {
142+
printf("FAILED: execute command: %s, err %d\n", test_script, -errno);
143+
goto err;
144+
}
138145

139146
rv = bpf_map_lookup_elem(bpf_map__fd(global_map), &key, &g);
140147
if (rv != 0) {

0 commit comments

Comments
 (0)