Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks
@ 2025-03-24  6:42 Malaya Kumar Rout
  2025-04-01  6:46 ` Hou Tao
  2025-04-04 15:52 ` Andrii Nakryiko
  0 siblings, 2 replies; 11+ messages in thread
From: Malaya Kumar Rout @ 2025-03-24  6:42 UTC (permalink / raw)
  To: malayarout91
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan, Geliang Tang, bpf, linux-kselftest,
	linux-kernel

Static Analyis for bench_htab_mem.c with cppcheck:error
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]

fix the issue  by closing the file descriptor (fd & tc) when
read & fgets operation fails.

Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
 tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
 tools/testing/selftests/bpf/prog_tests/sk_assign.c  | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 926ee822143e..59746fd2c23a 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
 	got = read(fd, buf, sizeof(buf) - 1);
 	if (got <= 0) {
 		*value = 0;
+		close(fd);
 		return;
 	}
 	buf[got] = 0;
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 0b9bd1d6f7cc..10a0ab954b8a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -37,8 +37,10 @@ configure_stack(void)
 	tc = popen("tc -V", "r");
 	if (CHECK_FAIL(!tc))
 		return false;
-	if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
+	if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
+		pclose(tc);
 		return false;
+	}
 	if (strstr(tc_version, ", libbpf "))
 		prog = "test_sk_assign_libbpf.bpf.o";
 	else
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-04-09 23:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24  6:42 [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks Malaya Kumar Rout
2025-04-01  6:46 ` Hou Tao
2025-04-04 15:52 ` Andrii Nakryiko
2025-04-05  5:59   ` malaya kumar rout
2025-04-07  1:36     ` Hou Tao
2025-04-07  3:40       ` malaya kumar rout
2025-04-07 10:01         ` Re:[PATCH v2] " Malaya Kumar Rout
2025-04-08  2:30           ` [PATCH " Hou Tao
2025-04-08 18:31             ` Re:[PATCH RESEND bpf-next " Malaya Kumar Rout
2025-04-09 23:27               ` [PATCH " Andrii Nakryiko
2025-04-09 23:30                 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox