Commit a37338aa authored by Riccardo Mancini's avatar Riccardo Mancini Committed by Arnaldo Carvalho de Melo

perf report: Free generated help strings for sort option

ASan reports the memory leak of the strings allocated by sort_help() when
running perf report.

This patch changes the returned pointer to char* (instead of const
char*), saves it in a temporary variable, and finally deallocates it at
function exit.
Signed-off-by: default avatarRiccardo Mancini <rickyman7@gmail.com>
Fixes: 702fb9b4 ("perf report: Show all sort keys in help output")
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/a38b13f02812a8a6759200b9063c6191337f44d4.1626343282.git.rickyman7@gmail.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent da6b7c6c
...@@ -1175,6 +1175,8 @@ int cmd_report(int argc, const char **argv) ...@@ -1175,6 +1175,8 @@ int cmd_report(int argc, const char **argv)
.annotation_opts = annotation__default_options, .annotation_opts = annotation__default_options,
.skip_empty = true, .skip_empty = true,
}; };
char *sort_order_help = sort_help("sort by key(s):");
char *field_order_help = sort_help("output field(s): overhead period sample ");
const struct option options[] = { const struct option options[] = {
OPT_STRING('i', "input", &input_name, "file", OPT_STRING('i', "input", &input_name, "file",
"input file name"), "input file name"),
...@@ -1209,9 +1211,9 @@ int cmd_report(int argc, const char **argv) ...@@ -1209,9 +1211,9 @@ int cmd_report(int argc, const char **argv)
OPT_BOOLEAN(0, "header-only", &report.header_only, OPT_BOOLEAN(0, "header-only", &report.header_only,
"Show only data header."), "Show only data header."),
OPT_STRING('s', "sort", &sort_order, "key[,key2...]", OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
sort_help("sort by key(s):")), sort_order_help),
OPT_STRING('F', "fields", &field_order, "key[,keys...]", OPT_STRING('F', "fields", &field_order, "key[,keys...]",
sort_help("output field(s): overhead period sample ")), field_order_help),
OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization, OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
"Show sample percentage for different cpu modes"), "Show sample percentage for different cpu modes"),
OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization, OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
...@@ -1344,11 +1346,11 @@ int cmd_report(int argc, const char **argv) ...@@ -1344,11 +1346,11 @@ int cmd_report(int argc, const char **argv)
char sort_tmp[128]; char sort_tmp[128];
if (ret < 0) if (ret < 0)
return ret; goto exit;
ret = perf_config(report__config, &report); ret = perf_config(report__config, &report);
if (ret) if (ret)
return ret; goto exit;
argc = parse_options(argc, argv, options, report_usage, 0); argc = parse_options(argc, argv, options, report_usage, 0);
if (argc) { if (argc) {
...@@ -1362,8 +1364,10 @@ int cmd_report(int argc, const char **argv) ...@@ -1362,8 +1364,10 @@ int cmd_report(int argc, const char **argv)
report.symbol_filter_str = argv[0]; report.symbol_filter_str = argv[0];
} }
if (annotate_check_args(&report.annotation_opts) < 0) if (annotate_check_args(&report.annotation_opts) < 0) {
return -EINVAL; ret = -EINVAL;
goto exit;
}
if (report.mmaps_mode) if (report.mmaps_mode)
report.tasks_mode = true; report.tasks_mode = true;
...@@ -1377,12 +1381,14 @@ int cmd_report(int argc, const char **argv) ...@@ -1377,12 +1381,14 @@ int cmd_report(int argc, const char **argv)
if (symbol_conf.vmlinux_name && if (symbol_conf.vmlinux_name &&
access(symbol_conf.vmlinux_name, R_OK)) { access(symbol_conf.vmlinux_name, R_OK)) {
pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name); pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
return -EINVAL; ret = -EINVAL;
goto exit;
} }
if (symbol_conf.kallsyms_name && if (symbol_conf.kallsyms_name &&
access(symbol_conf.kallsyms_name, R_OK)) { access(symbol_conf.kallsyms_name, R_OK)) {
pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name); pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
return -EINVAL; ret = -EINVAL;
goto exit;
} }
if (report.inverted_callchain) if (report.inverted_callchain)
...@@ -1406,12 +1412,14 @@ int cmd_report(int argc, const char **argv) ...@@ -1406,12 +1412,14 @@ int cmd_report(int argc, const char **argv)
repeat: repeat:
session = perf_session__new(&data, false, &report.tool); session = perf_session__new(&data, false, &report.tool);
if (IS_ERR(session)) if (IS_ERR(session)) {
return PTR_ERR(session); ret = PTR_ERR(session);
goto exit;
}
ret = evswitch__init(&report.evswitch, session->evlist, stderr); ret = evswitch__init(&report.evswitch, session->evlist, stderr);
if (ret) if (ret)
return ret; goto exit;
if (zstd_init(&(session->zstd_data), 0) < 0) if (zstd_init(&(session->zstd_data), 0) < 0)
pr_warning("Decompression initialization failed. Reported data may be incomplete.\n"); pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
...@@ -1646,5 +1654,8 @@ int cmd_report(int argc, const char **argv) ...@@ -1646,5 +1654,8 @@ int cmd_report(int argc, const char **argv)
zstd_fini(&(session->zstd_data)); zstd_fini(&(session->zstd_data));
perf_session__delete(session); perf_session__delete(session);
exit:
free(sort_order_help);
free(field_order_help);
return ret; return ret;
} }
...@@ -3370,7 +3370,7 @@ static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int ...@@ -3370,7 +3370,7 @@ static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int
add_key(sb, s[i].name, llen); add_key(sb, s[i].name, llen);
} }
const char *sort_help(const char *prefix) char *sort_help(const char *prefix)
{ {
struct strbuf sb; struct strbuf sb;
char *s; char *s;
......
...@@ -302,7 +302,7 @@ void reset_output_field(void); ...@@ -302,7 +302,7 @@ void reset_output_field(void);
void sort__setup_elide(FILE *fp); void sort__setup_elide(FILE *fp);
void perf_hpp__set_elide(int idx, bool elide); void perf_hpp__set_elide(int idx, bool elide);
const char *sort_help(const char *prefix); char *sort_help(const char *prefix);
int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset); int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment