Skip to content

Commit 316e5fd

Browse files
committed
selftests/bpf: Close the file descriptor to avoid resource leaks
JIRA: https://issues.redhat.com/browse/RHEL-78203 commit be2fea9 Author: Malaya Kumar Rout <malayarout91@gmail.com> Date: Mon Apr 21 23:14:05 2025 +0530 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 <malayarout91@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250421174405.26080-1-malayarout91@gmail.com Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent ed166c9 commit 316e5fd

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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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)