[PATCH] bpf: The main function in the file tools/bpf/bpf_dbg.c does not call fclose() to close the opened files at the end, leading to issues such as memory leaks.

liujing posted 1 patch 1 week, 2 days ago
tools/bpf/bpf_dbg.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
[PATCH] bpf: The main function in the file tools/bpf/bpf_dbg.c does not call fclose() to close the opened files at the end, leading to issues such as memory leaks.
Posted by liujing 1 week, 2 days ago
Signed-off-by: liujing <liujing@cmss.chinamobile.com>
---
 tools/bpf/bpf_dbg.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpf_dbg.c b/tools/bpf/bpf_dbg.c
index 00e560a17baf..ac834b6d78a8 100644
--- a/tools/bpf/bpf_dbg.c
+++ b/tools/bpf/bpf_dbg.c
@@ -1388,11 +1388,18 @@ static int run_shell_loop(FILE *fin, FILE *fout)
 int main(int argc, char **argv)
 {
 	FILE *fin = NULL, *fout = NULL;
+	int result;
 
 	if (argc >= 2)
 		fin = fopen(argv[1], "r");
 	if (argc >= 3)
 		fout = fopen(argv[2], "w");
 
-	return run_shell_loop(fin ? : stdin, fout ? : stdout);
+	result = run_shell_loop(fin ? : stdin, fout ? : stdout);
+
+	if (fin && fin != stdin)
+		fclose(fin);
+	if (fout && fout != stdout)
+		fclose(fout);
+	return result;
 }
-- 
2.27.0
Re: [PATCH] bpf: The main function in the file tools/bpf/bpf_dbg.c does not call fclose() to close the opened files at the end, leading to issues such as memory leaks.
Posted by Song Liu 1 week, 2 days ago
A few logistics:

1. Please tag your patch with [PATCH bpf-next <version>] or
[PATCH bpf <version>] so that the CI can apply it to the right tree.
2. Please write better commit log. The subject should include "what";
while the "why" part should go to the later part of the commit log.
For example:

bpf_debug: Close opened files

Add fclose calls to the main function to openeed files, so that we
do not memory leak.

On Mon, Sep 22, 2025 at 10:22 AM liujing <liujing@cmss.chinamobile.com> wrote:
>
> Signed-off-by: liujing <liujing@cmss.chinamobile.com>

3. Please sign off with your full real name.

> ---
>  tools/bpf/bpf_dbg.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/bpf_dbg.c b/tools/bpf/bpf_dbg.c
> index 00e560a17baf..ac834b6d78a8 100644
> --- a/tools/bpf/bpf_dbg.c
> +++ b/tools/bpf/bpf_dbg.c
> @@ -1388,11 +1388,18 @@ static int run_shell_loop(FILE *fin, FILE *fout)
>  int main(int argc, char **argv)
>  {
>         FILE *fin = NULL, *fout = NULL;
> +       int result;
>
>         if (argc >= 2)
>                 fin = fopen(argv[1], "r");
>         if (argc >= 3)
>                 fout = fopen(argv[2], "w");
>
> -       return run_shell_loop(fin ? : stdin, fout ? : stdout);
> +       result = run_shell_loop(fin ? : stdin, fout ? : stdout);
> +
> +       if (fin && fin != stdin)

We can simply do "if (fin)", right?