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

Commit ab656dd

Browse files
adi-g15-ibmgregkh
authored andcommitted
libsubcmd: Don't free the usage string
[ Upstream commit 1a5efc9 ] 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 <[email protected]> Signed-off-by: Aditya Gupta <[email protected]> Acked-by: Namhyung Kim <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Athira Rajeev <[email protected]> Cc: Disha Goel <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 03cec19 commit ab656dd

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
lines changed

tools/lib/subcmd/parse-options.c

Lines changed: 3 additions & 5 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,8 @@ int cmd_kmem(int argc, const char **argv)
20582058

20592059
out_delete:
20602060
perf_session__delete(session);
2061+
/* free usage string allocated by parse_options_subcommand */
2062+
free((void *)kmem_usage[0]);
20612063

20622064
return ret;
20632065
}

tools/perf/builtin-kvm.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,5 +2187,8 @@ int cmd_kvm(int argc, const char **argv)
21872187
else
21882188
usage_with_options(kvm_usage, kvm_options);
21892189

2190+
/* free usage string allocated by parse_options_subcommand */
2191+
free((void *)kvm_usage[0]);
2192+
21902193
return 0;
21912194
}

tools/perf/builtin-kwork.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,5 +1853,8 @@ int cmd_kwork(int argc, const char **argv)
18531853
} else
18541854
usage_with_options(kwork_usage, kwork_options);
18551855

1856+
/* free usage string allocated by parse_options_subcommand */
1857+
free((void *)kwork_usage[0]);
1858+
18561859
return 0;
18571860
}

tools/perf/builtin-lock.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,9 @@ int cmd_lock(int argc, const char **argv)
26222622
usage_with_options(lock_usage, lock_options);
26232623
}
26242624

2625+
/* free usage string allocated by parse_options_subcommand */
2626+
free((void *)lock_usage[0]);
2627+
26252628
zfree(&lockhash_table);
26262629
return rc;
26272630
}

tools/perf/builtin-mem.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,5 +518,8 @@ int cmd_mem(int argc, const char **argv)
518518
else
519519
usage_with_options(mem_usage, mem_options);
520520

521+
/* free usage string allocated by parse_options_subcommand */
522+
free((void *)mem_usage[0]);
523+
521524
return 0;
522525
}

tools/perf/builtin-sched.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,5 +3737,8 @@ int cmd_sched(int argc, const char **argv)
37373737
usage_with_options(sched_usage, sched_options);
37383738
}
37393739

3740+
/* free usage string allocated by parse_options_subcommand */
3741+
free((void *)sched_usage[0]);
3742+
37403743
return 0;
37413744
}

0 commit comments

Comments
 (0)