linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] libsubcmd: Fix parse-options memory leak
@ 2024-05-09  5:20 Ian Rogers
  2024-05-09  5:34 ` Ian Rogers
  2024-05-10 14:17 ` Arnaldo Carvalho de Melo
  0 siblings, 2 replies; 3+ messages in thread
From: Ian Rogers @ 2024-05-09  5:20 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, linux-kernel,
	linux-perf-users

If a usage string is built in parse_options_subcommand, also free it.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/subcmd/parse-options.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
index 9fa75943f2ed..d943d78b787e 100644
--- a/tools/lib/subcmd/parse-options.c
+++ b/tools/lib/subcmd/parse-options.c
@@ -633,11 +633,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
 			const char *const subcommands[], const char *usagestr[], int flags)
 {
 	struct parse_opt_ctx_t ctx;
+	char *buf = NULL;
 
 	/* build usage string if it's not provided */
 	if (subcommands && !usagestr[0]) {
-		char *buf = NULL;
-
 		astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
 
 		for (int i = 0; subcommands[i]; i++) {
@@ -679,7 +678,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
 			astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
 		usage_with_options(usagestr, options);
 	}
-
+	if (buf) {
+		usagestr[0] = NULL;
+		free(buf);
+	}
 	return parse_options_end(&ctx);
 }
 
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog


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

* Re: [PATCH v1] libsubcmd: Fix parse-options memory leak
  2024-05-09  5:20 [PATCH v1] libsubcmd: Fix parse-options memory leak Ian Rogers
@ 2024-05-09  5:34 ` Ian Rogers
  2024-05-10 14:17 ` Arnaldo Carvalho de Melo
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Rogers @ 2024-05-09  5:34 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, linux-kernel,
	linux-perf-users

On Wed, May 8, 2024 at 10:20 PM Ian Rogers <irogers@google.com> wrote:
>
> If a usage string is built in parse_options_subcommand, also free it.
>
> Signed-off-by: Ian Rogers <irogers@google.com>

Sorry, belated:
Fixes: 901421a5bdf6 ("perf tools: Remove subcmd dependencies on strbuf")

Thanks,
Ian

> ---
>  tools/lib/subcmd/parse-options.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> index 9fa75943f2ed..d943d78b787e 100644
> --- a/tools/lib/subcmd/parse-options.c
> +++ b/tools/lib/subcmd/parse-options.c
> @@ -633,11 +633,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
>                         const char *const subcommands[], const char *usagestr[], int flags)
>  {
>         struct parse_opt_ctx_t ctx;
> +       char *buf = NULL;
>
>         /* build usage string if it's not provided */
>         if (subcommands && !usagestr[0]) {
> -               char *buf = NULL;
> -
>                 astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
>
>                 for (int i = 0; subcommands[i]; i++) {
> @@ -679,7 +678,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
>                         astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
>                 usage_with_options(usagestr, options);
>         }
> -
> +       if (buf) {
> +               usagestr[0] = NULL;
> +               free(buf);
> +       }
>         return parse_options_end(&ctx);
>  }
>
> --
> 2.45.0.rc1.225.g2a3ae87e7f-goog
>

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

* Re: [PATCH v1] libsubcmd: Fix parse-options memory leak
  2024-05-09  5:20 [PATCH v1] libsubcmd: Fix parse-options memory leak Ian Rogers
  2024-05-09  5:34 ` Ian Rogers
@ 2024-05-10 14:17 ` Arnaldo Carvalho de Melo
  1 sibling, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2024-05-10 14:17 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Adrian Hunter, Kan Liang,
	linux-kernel, linux-perf-users

On Wed, May 08, 2024 at 10:20:15PM -0700, Ian Rogers wrote:
> If a usage string is built in parse_options_subcommand, also free it.

Thanks, applied to perf-tools-next,

- Arnaldo
 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/lib/subcmd/parse-options.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> index 9fa75943f2ed..d943d78b787e 100644
> --- a/tools/lib/subcmd/parse-options.c
> +++ b/tools/lib/subcmd/parse-options.c
> @@ -633,11 +633,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
>  			const char *const subcommands[], const char *usagestr[], int flags)
>  {
>  	struct parse_opt_ctx_t ctx;
> +	char *buf = NULL;
>  
>  	/* build usage string if it's not provided */
>  	if (subcommands && !usagestr[0]) {
> -		char *buf = NULL;
> -
>  		astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
>  
>  		for (int i = 0; subcommands[i]; i++) {
> @@ -679,7 +678,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
>  			astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
>  		usage_with_options(usagestr, options);
>  	}
> -
> +	if (buf) {
> +		usagestr[0] = NULL;
> +		free(buf);
> +	}
>  	return parse_options_end(&ctx);
>  }
>  
> -- 
> 2.45.0.rc1.225.g2a3ae87e7f-goog

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

end of thread, other threads:[~2024-05-10 14:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-09  5:20 [PATCH v1] libsubcmd: Fix parse-options memory leak Ian Rogers
2024-05-09  5:34 ` Ian Rogers
2024-05-10 14:17 ` Arnaldo Carvalho de Melo

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).