Skip to content

Commit a37338a

Browse files
Manciukicacmel
authored andcommitted
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: Riccardo Mancini <[email protected]> Fixes: 702fb9b ("perf report: Show all sort keys in help output") Cc: Andi Kleen <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lore.kernel.org/lkml/a38b13f02812a8a6759200b9063c6191337f44d4.1626343282.git.rickyman7@gmail.com Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent da6b7c6 commit a37338a

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

tools/perf/builtin-report.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,8 @@ int cmd_report(int argc, const char **argv)
11751175
.annotation_opts = annotation__default_options,
11761176
.skip_empty = true,
11771177
};
1178+
char *sort_order_help = sort_help("sort by key(s):");
1179+
char *field_order_help = sort_help("output field(s): overhead period sample ");
11781180
const struct option options[] = {
11791181
OPT_STRING('i', "input", &input_name, "file",
11801182
"input file name"),
@@ -1209,9 +1211,9 @@ int cmd_report(int argc, const char **argv)
12091211
OPT_BOOLEAN(0, "header-only", &report.header_only,
12101212
"Show only data header."),
12111213
OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1212-
sort_help("sort by key(s):")),
1214+
sort_order_help),
12131215
OPT_STRING('F', "fields", &field_order, "key[,keys...]",
1214-
sort_help("output field(s): overhead period sample ")),
1216+
field_order_help),
12151217
OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
12161218
"Show sample percentage for different cpu modes"),
12171219
OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
@@ -1344,11 +1346,11 @@ int cmd_report(int argc, const char **argv)
13441346
char sort_tmp[128];
13451347

13461348
if (ret < 0)
1347-
return ret;
1349+
goto exit;
13481350

13491351
ret = perf_config(report__config, &report);
13501352
if (ret)
1351-
return ret;
1353+
goto exit;
13521354

13531355
argc = parse_options(argc, argv, options, report_usage, 0);
13541356
if (argc) {
@@ -1362,8 +1364,10 @@ int cmd_report(int argc, const char **argv)
13621364
report.symbol_filter_str = argv[0];
13631365
}
13641366

1365-
if (annotate_check_args(&report.annotation_opts) < 0)
1366-
return -EINVAL;
1367+
if (annotate_check_args(&report.annotation_opts) < 0) {
1368+
ret = -EINVAL;
1369+
goto exit;
1370+
}
13671371

13681372
if (report.mmaps_mode)
13691373
report.tasks_mode = true;
@@ -1377,12 +1381,14 @@ int cmd_report(int argc, const char **argv)
13771381
if (symbol_conf.vmlinux_name &&
13781382
access(symbol_conf.vmlinux_name, R_OK)) {
13791383
pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
1380-
return -EINVAL;
1384+
ret = -EINVAL;
1385+
goto exit;
13811386
}
13821387
if (symbol_conf.kallsyms_name &&
13831388
access(symbol_conf.kallsyms_name, R_OK)) {
13841389
pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
1385-
return -EINVAL;
1390+
ret = -EINVAL;
1391+
goto exit;
13861392
}
13871393

13881394
if (report.inverted_callchain)
@@ -1406,12 +1412,14 @@ int cmd_report(int argc, const char **argv)
14061412

14071413
repeat:
14081414
session = perf_session__new(&data, false, &report.tool);
1409-
if (IS_ERR(session))
1410-
return PTR_ERR(session);
1415+
if (IS_ERR(session)) {
1416+
ret = PTR_ERR(session);
1417+
goto exit;
1418+
}
14111419

14121420
ret = evswitch__init(&report.evswitch, session->evlist, stderr);
14131421
if (ret)
1414-
return ret;
1422+
goto exit;
14151423

14161424
if (zstd_init(&(session->zstd_data), 0) < 0)
14171425
pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
@@ -1646,5 +1654,8 @@ int cmd_report(int argc, const char **argv)
16461654

16471655
zstd_fini(&(session->zstd_data));
16481656
perf_session__delete(session);
1657+
exit:
1658+
free(sort_order_help);
1659+
free(field_order_help);
16491660
return ret;
16501661
}

tools/perf/util/sort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3370,7 +3370,7 @@ static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int
33703370
add_key(sb, s[i].name, llen);
33713371
}
33723372

3373-
const char *sort_help(const char *prefix)
3373+
char *sort_help(const char *prefix)
33743374
{
33753375
struct strbuf sb;
33763376
char *s;

tools/perf/util/sort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void reset_output_field(void);
302302
void sort__setup_elide(FILE *fp);
303303
void perf_hpp__set_elide(int idx, bool elide);
304304

305-
const char *sort_help(const char *prefix);
305+
char *sort_help(const char *prefix);
306306

307307
int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
308308

0 commit comments

Comments
 (0)