Skip to content

Commit be2fea9

Browse files
malayarout91anakryiko
authored andcommitted
selftests/bpf: Close the file descriptor to avoid resource leaks
Static analysis found an issue in bench_htab_mem.c and sk_assign.c cppcheck output before this patch: tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3: error: Resource leak: fd [resourceLeak] tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3: error: Resource leak: tc [resourceLeak] cppcheck output after this patch: No resource leaks found Fix the issue by closing the file descriptors fd and tc. Signed-off-by: Malaya Kumar Rout <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5709be4 commit be2fea9

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

tools/testing/selftests/bpf/benchs/bench_htab_mem.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,14 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
279279
}
280280

281281
got = read(fd, buf, sizeof(buf) - 1);
282+
close(fd);
282283
if (got <= 0) {
283284
*value = 0;
284285
return;
285286
}
286287
buf[got] = 0;
287288

288289
*value = strtoull(buf, NULL, 0);
289-
290-
close(fd);
291290
}
292291

293292
static void htab_mem_measure(struct bench_res *res)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ configure_stack(void)
3737
tc = popen("tc -V", "r");
3838
if (CHECK_FAIL(!tc))
3939
return false;
40-
if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
40+
if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
41+
pclose(tc);
4142
return false;
43+
}
4244
if (strstr(tc_version, ", libbpf "))
4345
prog = "test_sk_assign_libbpf.bpf.o";
4446
else

0 commit comments

Comments
 (0)