Skip to contentSkip to conversation

libsubcmd: Fix parse-options memory leak

PATCH Applied Ian Rogers 2 participants 3 messages May 9, 2024 – May 10, 2024

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 libsubcmd: Fix parse-options memory leak

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

---
 tools/lib/subcmd/parse-options.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/lib/subcmd/parse-options.c +5 -3
index 9fa75943f2ed..d943d78b787e 100644
@@ -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);
 }
 
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#1 lore raw
Quoted 4 lines · Ian Rogers

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

Sorry, belated:

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

Thanks,
Ian

Quoted 36 lines
---
 tools/lib/subcmd/parse-options.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/lib/subcmd/parse-options.c +5 -4
index 9fa75943f2ed..d943d78b787e 100644
@@ -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

#2 lore raw
In reply to Ian Rogers
Quoted 1 line · Ian Rogers

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

Thanks, applied to perf-tools-next,

- Arnaldo

Quoted 36 lines
---
 tools/lib/subcmd/parse-options.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/lib/subcmd/parse-options.c +5 -3
index 9fa75943f2ed..d943d78b787e 100644
@@ -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);
 }
 
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog