bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpftool: Fix JSON writer resource leak in version command
@ 2025-06-16 13:50 Yuan Chen
  2025-06-16 14:24 ` Quentin Monnet
  0 siblings, 1 reply; 5+ messages in thread
From: Yuan Chen @ 2025-06-16 13:50 UTC (permalink / raw)
  To: qmo, ast; +Cc: bpf, linux-kernel, chenyuan_fl, chenyuan

From: chenyuan <chenyuan@kylinos.cn>

When using `bpftool --version -j/-p`, the JSON writer object
created in do_version() was not properly destroyed after use.
This caused a memory leak each time the version command was
executed with JSON output.

Fix: 004b45c0e51a (tools: bpftool: provide JSON output for all possible commands)
Signed-off-by: chenyuan <chenyuan@kylinos.cn>
---
 tools/bpf/bpftool/main.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index cd5963cb6058..c8838196a3bd 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -164,6 +164,7 @@ static int do_version(int argc, char **argv)
 		jsonw_end_object(json_wtr);	/* features */
 
 		jsonw_end_object(json_wtr);	/* root object */
+		jsonw_destroy(&json_wtr);
 	} else {
 		unsigned int nb_features = 0;
 
-- 
2.44.0


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

* Re: [PATCH] bpftool: Fix JSON writer resource leak in version command
  2025-06-16 13:50 [PATCH] bpftool: Fix JSON writer resource leak in version command Yuan Chen
@ 2025-06-16 14:24 ` Quentin Monnet
  0 siblings, 0 replies; 5+ messages in thread
From: Quentin Monnet @ 2025-06-16 14:24 UTC (permalink / raw)
  To: Yuan Chen, ast; +Cc: bpf, linux-kernel, chenyuan

2025-06-16 09:50 UTC-0400 ~ Yuan Chen <chenyuan_fl@163.com>
> From: chenyuan <chenyuan@kylinos.cn>
> 
> When using `bpftool --version -j/-p`, the JSON writer object
> created in do_version() was not properly destroyed after use.
> This caused a memory leak each time the version command was
> executed with JSON output.
> 
> Fix: 004b45c0e51a (tools: bpftool: provide JSON output for all possible commands)
> Signed-off-by: chenyuan <chenyuan@kylinos.cn>
> ---
>  tools/bpf/bpftool/main.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
> index cd5963cb6058..c8838196a3bd 100644
> --- a/tools/bpf/bpftool/main.c
> +++ b/tools/bpf/bpftool/main.c
> @@ -164,6 +164,7 @@ static int do_version(int argc, char **argv)
>  		jsonw_end_object(json_wtr);	/* features */
>  
>  		jsonw_end_object(json_wtr);	/* root object */
> +		jsonw_destroy(&json_wtr);
>  	} else {
>  		unsigned int nb_features = 0;
>  


Good catch, but the fix is not correct:

	$ ./bpftool version -j >/dev/null
	zsh: segmentation fault (core dumped)

This is because we already run jsonw_destroy() at the end of the main()
function when printing the version with "bpftool version", the command
(and not the -V or --version options).

One option would be to add a call at the end of main(), when we call
do_version() because "version_requested" is set:

	if (version_requested) {
		do_version(argc, argv);
		if (json_output)
			jsonw_destroy(&json_wtr);
		return 0;
	}

Thanks,
Quentin

pw-bot: cr

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

* [PATCH] bpftool: Fix JSON writer resource leak in version command
@ 2025-06-16 15:27 Yuan Chen
  2025-06-16 15:50 ` Quentin Monnet
  2025-06-16 21:19 ` Andrii Nakryiko
  0 siblings, 2 replies; 5+ messages in thread
From: Yuan Chen @ 2025-06-16 15:27 UTC (permalink / raw)
  To: qmo, ast; +Cc: bpf, linux-kernel, chenyuan_fl, Yuan Chen

From: Yuan Chen <chenyuan@kylinos.cn>

When using `bpftool --version -j/-p`, the JSON writer object
created in do_version() was not properly destroyed after use.
This caused a memory leak each time the version command was
executed with JSON output.

Fix: 004b45c0e51a (tools: bpftool: provide JSON output for all possible commands)
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
Suggested-by: Quentin Monnet <qmo@kernel.org>
---
 tools/bpf/bpftool/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index cd5963cb6058..33c68eccd2c3 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -533,8 +533,12 @@ int main(int argc, char **argv)
 	if (argc < 0)
 		usage();
 
-	if (version_requested)
-		return do_version(argc, argv);
+	if (version_requested) {
+		ret = do_version(argc, argv);
+		if (json_output)
+			jsonw_destroy(&json_wtr);
+		return ret;
+	}
 
 	ret = cmd_select(commands, argc, argv, do_help);
 
-- 
2.44.0


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

* Re: [PATCH] bpftool: Fix JSON writer resource leak in version command
  2025-06-16 15:27 Yuan Chen
@ 2025-06-16 15:50 ` Quentin Monnet
  2025-06-16 21:19 ` Andrii Nakryiko
  1 sibling, 0 replies; 5+ messages in thread
From: Quentin Monnet @ 2025-06-16 15:50 UTC (permalink / raw)
  To: Yuan Chen, ast; +Cc: bpf, linux-kernel, Yuan Chen

2025-06-16 11:27 UTC-0400 ~ Yuan Chen <chenyuan_fl@163.com>
> From: Yuan Chen <chenyuan@kylinos.cn>
> 
> When using `bpftool --version -j/-p`, the JSON writer object
> created in do_version() was not properly destroyed after use.
> This caused a memory leak each time the version command was
> executed with JSON output.
> 
> Fix: 004b45c0e51a (tools: bpftool: provide JSON output for all possible commands)
> Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
> Suggested-by: Quentin Monnet <qmo@kernel.org>

Thanks a lot! Please, for future patches, state the version of your
patch (if > 1) in the prefix of your email subject:

	[PATCH bpf-next v2] bpftool: Fix JSON writer...

Reviewed-by: Quentin Monnet <qmo@kernel.org>

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

* Re: [PATCH] bpftool: Fix JSON writer resource leak in version command
  2025-06-16 15:27 Yuan Chen
  2025-06-16 15:50 ` Quentin Monnet
@ 2025-06-16 21:19 ` Andrii Nakryiko
  1 sibling, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2025-06-16 21:19 UTC (permalink / raw)
  To: Yuan Chen; +Cc: qmo, ast, bpf, linux-kernel, Yuan Chen

On Mon, Jun 16, 2025 at 8:30 AM Yuan Chen <chenyuan_fl@163.com> wrote:
>
> From: Yuan Chen <chenyuan@kylinos.cn>
>
> When using `bpftool --version -j/-p`, the JSON writer object
> created in do_version() was not properly destroyed after use.
> This caused a memory leak each time the version command was
> executed with JSON output.
>
> Fix: 004b45c0e51a (tools: bpftool: provide JSON output for all possible commands)
> Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
> Suggested-by: Quentin Monnet <qmo@kernel.org>
> ---
>  tools/bpf/bpftool/main.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
> index cd5963cb6058..33c68eccd2c3 100644
> --- a/tools/bpf/bpftool/main.c
> +++ b/tools/bpf/bpftool/main.c
> @@ -533,8 +533,12 @@ int main(int argc, char **argv)
>         if (argc < 0)
>                 usage();
>
> -       if (version_requested)
> -               return do_version(argc, argv);
> +       if (version_requested) {
> +               ret = do_version(argc, argv);
> +               if (json_output)
> +                       jsonw_destroy(&json_wtr);
> +               return ret;
> +       }

there is also btf__free(base_btf), so instead of jsonw_destroy copy,
let's maybe just do

if (version_requested)
    ret = do_version(...);
else
    ret = cmd_select(...);

jsonw_destroy, btf__free...

return ret;

pw-bot: cr


>
>         ret = cmd_select(commands, argc, argv, do_help);
>
> --
> 2.44.0
>
>

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

end of thread, other threads:[~2025-06-16 21:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-16 13:50 [PATCH] bpftool: Fix JSON writer resource leak in version command Yuan Chen
2025-06-16 14:24 ` Quentin Monnet
  -- strict thread matches above, loose matches on Subject: below --
2025-06-16 15:27 Yuan Chen
2025-06-16 15:50 ` Quentin Monnet
2025-06-16 21:19 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).