Skip to content

Commit 1a5efc9

Browse files
adi-g15-ibmacmel
authored andcommitted
libsubcmd: Don't free the usage string
Currently, commands which depend on 'parse_options_subcommand()' don't show the usage string, and instead show '(null)' $ ./perf sched Usage: (null) -D, --dump-raw-trace dump raw trace in ASCII -f, --force don't complain, do it -i, --input <file> input file name -v, --verbose be more verbose (show symbol address, etc) 'parse_options_subcommand()' is generally expected to initialise the usage string, with information in the passed 'subcommands[]' array This behaviour was changed in: 230a7a7 ("libsubcmd: Fix parse-options memory leak") Where the generated usage string is deallocated, and usage[0] string is reassigned as NULL. As discussed in [1], free the allocated usage string in the main function itself, and don't reset usage string to NULL in parse_options_subcommand With this change, the behaviour is restored. $ ./perf sched Usage: perf sched [<options>] {record|latency|map|replay|script|timehist} -D, --dump-raw-trace dump raw trace in ASCII -f, --force don't complain, do it -i, --input <file> input file name -v, --verbose be more verbose (show symbol address, etc) [1]: https://lore.kernel.org/linux-perf-users/htq5vhx6piet4nuq2mmhk7fs2bhfykv52dbppwxmo3s7du2odf@styd27tioc6e/ Fixes: 230a7a7 ("libsubcmd: Fix parse-options memory leak") Suggested-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Aditya Gupta <adityag@linux.ibm.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Disha Goel <disgoel@linux.vnet.ibm.com> Cc: Ian Rogers <irogers@google.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Madhavan Srinivasan <maddy@linux.ibm.com> Cc: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/r/20240904061836.55873-2-adityag@linux.ibm.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent fa6cc3f commit 1a5efc9

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
lines changed

tools/lib/subcmd/parse-options.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,11 @@ 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;
637636

638637
/* build usage string if it's not provided */
639638
if (subcommands && !usagestr[0]) {
639+
char *buf = NULL;
640+
640641
astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
641642

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

tools/perf/builtin-kmem.c

+2
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,8 @@ int cmd_kmem(int argc, const char **argv)
20572057

20582058
out_delete:
20592059
perf_session__delete(session);
2060+
/* free usage string allocated by parse_options_subcommand */
2061+
free((void *)kmem_usage[0]);
20602062

20612063
return ret;
20622064
}

tools/perf/builtin-kvm.c

+3
Original file line numberDiff line numberDiff line change
@@ -2184,5 +2184,8 @@ int cmd_kvm(int argc, const char **argv)
21842184
else
21852185
usage_with_options(kvm_usage, kvm_options);
21862186

2187+
/* free usage string allocated by parse_options_subcommand */
2188+
free((void *)kvm_usage[0]);
2189+
21872190
return 0;
21882191
}

tools/perf/builtin-kwork.c

+3
Original file line numberDiff line numberDiff line change
@@ -2519,5 +2519,8 @@ int cmd_kwork(int argc, const char **argv)
25192519
} else
25202520
usage_with_options(kwork_usage, kwork_options);
25212521

2522+
/* free usage string allocated by parse_options_subcommand */
2523+
free((void *)kwork_usage[0]);
2524+
25222525
return 0;
25232526
}

tools/perf/builtin-lock.c

+3
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,9 @@ int cmd_lock(int argc, const char **argv)
27122712
usage_with_options(lock_usage, lock_options);
27132713
}
27142714

2715+
/* free usage string allocated by parse_options_subcommand */
2716+
free((void *)lock_usage[0]);
2717+
27152718
zfree(&lockhash_table);
27162719
return rc;
27172720
}

tools/perf/builtin-mem.c

+3
Original file line numberDiff line numberDiff line change
@@ -546,5 +546,8 @@ int cmd_mem(int argc, const char **argv)
546546
else
547547
usage_with_options(mem_usage, mem_options);
548548

549+
/* free usage string allocated by parse_options_subcommand */
550+
free((void *)mem_usage[0]);
551+
549552
return 0;
550553
}

tools/perf/builtin-sched.c

+3
Original file line numberDiff line numberDiff line change
@@ -3954,5 +3954,8 @@ int cmd_sched(int argc, const char **argv)
39543954
usage_with_options(sched_usage, sched_options);
39553955
}
39563956

3957+
/* free usage string allocated by parse_options_subcommand */
3958+
free((void *)sched_usage[0]);
3959+
39573960
return 0;
39583961
}

0 commit comments

Comments
 (0)