[PATCH] bpf: Optimize resource leakage problems

From: liujing
Date: Wed Nov 27 2024 - 21:47:48 EST


If fopen executes successfully in the main function, it does
not close the open file stream before the end of the function.

Signed-off-by: liujing <liujing@xxxxxxxxxxxxxxxxxxxx>

diff --git a/tools/bpf/bpf_dbg.c b/tools/bpf/bpf_dbg.c
index 00e560a17baf..2445dfb4fc46 100644
--- a/tools/bpf/bpf_dbg.c
+++ b/tools/bpf/bpf_dbg.c
@@ -1388,11 +1388,17 @@ 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)
+ fclose(fin);
+
+ if (fout)
+ fclose(fout);
+ return result;
}
--
2.27.0