From: Malaya Kumar Rout <malayarout91@gmail.com>
To: andrii.nakryiko@gmail.com, alexei.starovoitov@gmail.com
Cc: bpf@vger.kernel.org, Malaya Kumar Rout <malayarout91@gmail.com>
Subject: RE:[PATCH RESEND bpf-next v3] selftests/bpf: close the file descriptor to avoid resource leaks
Date: Thu, 10 Apr 2025 16:08:04 +0530 [thread overview]
Message-ID: <20250410103804.49250-1-malayarout91@gmail.com> (raw)
In-Reply-To: <CAADnVQJbBOK25Fx3zEG-ZH=zTFRfPNQye673b5TnpdTdMEXAUA@mail.gmail.com>
Static analysis found an issue in bench_htab_mem.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>
---
tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 3 +--
tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 926ee822143e..297e32390cd1 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -279,6 +279,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
}
got = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
if (got <= 0) {
*value = 0;
return;
@@ -286,8 +287,6 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
buf[got] = 0;
*value = strtoull(buf, NULL, 0);
-
- close(fd);
}
static void htab_mem_measure(struct bench_res *res)
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
next prev parent reply other threads:[~2025-04-10 10:38 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
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
2025-04-10 10:38 ` Malaya Kumar Rout [this message]
2025-04-10 17:32 ` [PATCH RESEND bpf-next v3] " Andrii Nakryiko
2025-04-12 18:44 ` malaya kumar rout
2025-04-15 23:15 ` Andrii Nakryiko
2025-04-21 17:48 ` malaya kumar rout
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250410103804.49250-1-malayarout91@gmail.com \
--to=malayarout91@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=bpf@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox