Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 230a7a7

Browse files
captain5050acmel
authored andcommitted
libsubcmd: Fix parse-options memory leak
If a usage string is built in parse_options_subcommand, also free it. Fixes: 901421a ("perf tools: Remove subcmd dependencies on strbuf") Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20240509052015.1914670-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 5ecab78 commit 230a7a7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tools/lib/subcmd/parse-options.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
633633
const char *const subcommands[], const char *usagestr[], int flags)
634634
{
635635
struct parse_opt_ctx_t ctx;
636+
char *buf = NULL;
636637

637638
/* build usage string if it's not provided */
638639
if (subcommands && !usagestr[0]) {
639-
char *buf = NULL;
640-
641640
astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
642641

643642
for (int i = 0; subcommands[i]; i++) {
@@ -679,7 +678,10 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
679678
astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
680679
usage_with_options(usagestr, options);
681680
}
682-
681+
if (buf) {
682+
usagestr[0] = NULL;
683+
free(buf);
684+
}
683685
return parse_options_end(&ctx);
684686
}
685687

0 commit comments

Comments
 (0)