linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Riccardo Mancini <rickyman7@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>, Jiri Olsa <jolsa@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Riccardo Mancini <rickyman7@gmail.com>
Subject: [PATCH 09/20] perf report: free generated help strings for sort option
Date: Thu, 15 Jul 2021 18:07:14 +0200	[thread overview]
Message-ID: <a38b13f02812a8a6759200b9063c6191337f44d4.1626343282.git.rickyman7@gmail.com> (raw)
In-Reply-To: <cover.1626343282.git.rickyman7@gmail.com>

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 <rickyman7@gmail.com>
---
 tools/perf/builtin-report.c | 33 ++++++++++++++++++++++-----------
 tools/perf/util/sort.c      |  2 +-
 tools/perf/util/sort.h      |  2 +-
 3 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 6386af6a2612367c..dc0364f671b97d26 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -1175,6 +1175,8 @@ int cmd_report(int argc, const char **argv)
 		.annotation_opts	 = annotation__default_options,
 		.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[] = {
 	OPT_STRING('i', "input", &input_name, "file",
 		    "input file name"),
@@ -1209,9 +1211,9 @@ int cmd_report(int argc, const char **argv)
 	OPT_BOOLEAN(0, "header-only", &report.header_only,
 		    "Show only data header."),
 	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...]",
-		   sort_help("output field(s): overhead period sample ")),
+		   field_order_help),
 	OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
 		    "Show sample percentage for different cpu modes"),
 	OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
@@ -1344,11 +1346,11 @@ int cmd_report(int argc, const char **argv)
 	char sort_tmp[128];
 
 	if (ret < 0)
-		return ret;
+		goto exit;
 
 	ret = perf_config(report__config, &report);
 	if (ret)
-		return ret;
+		goto exit;
 
 	argc = parse_options(argc, argv, options, report_usage, 0);
 	if (argc) {
@@ -1362,8 +1364,10 @@ int cmd_report(int argc, const char **argv)
 		report.symbol_filter_str = argv[0];
 	}
 
-	if (annotate_check_args(&report.annotation_opts) < 0)
-		return -EINVAL;
+	if (annotate_check_args(&report.annotation_opts) < 0) {
+		ret = -EINVAL;
+		goto exit;
+	}
 
 	if (report.mmaps_mode)
 		report.tasks_mode = true;
@@ -1377,12 +1381,14 @@ int cmd_report(int argc, const char **argv)
 	if (symbol_conf.vmlinux_name &&
 	    access(symbol_conf.vmlinux_name, R_OK)) {
 		pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto exit;
 	}
 	if (symbol_conf.kallsyms_name &&
 	    access(symbol_conf.kallsyms_name, R_OK)) {
 		pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto exit;
 	}
 
 	if (report.inverted_callchain)
@@ -1406,12 +1412,14 @@ int cmd_report(int argc, const char **argv)
 
 repeat:
 	session = perf_session__new(&data, false, &report.tool);
-	if (IS_ERR(session))
-		return PTR_ERR(session);
+	if (IS_ERR(session)) {
+		ret = PTR_ERR(session);
+		goto exit;
+	}
 
 	ret = evswitch__init(&report.evswitch, session->evlist, stderr);
 	if (ret)
-		return ret;
+		goto exit;
 
 	if (zstd_init(&(session->zstd_data), 0) < 0)
 		pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
@@ -1646,5 +1654,8 @@ int cmd_report(int argc, const char **argv)
 
 	zstd_fini(&(session->zstd_data));
 	perf_session__delete(session);
+exit:
+	free(sort_order_help);
+	free(field_order_help);
 	return ret;
 }
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 88ce47f2547e3558..568a88c001c6cb5a 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -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);
 }
 
-const char *sort_help(const char *prefix)
+char *sort_help(const char *prefix)
 {
 	struct strbuf sb;
 	char *s;
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
index 87a092645aa72e41..b67c469aba79587f 100644
--- a/tools/perf/util/sort.h
+++ b/tools/perf/util/sort.h
@@ -302,7 +302,7 @@ void reset_output_field(void);
 void sort__setup_elide(FILE *fp);
 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);
 
-- 
2.31.1


  parent reply	other threads:[~2021-07-15 16:07 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15 16:07 [PATCH 00/20] perf: fix several memory leaks reported by ASan on perf-test Riccardo Mancini
2021-07-15 16:07 ` [PATCH 01/20] perf nsinfo: fix refcounting Riccardo Mancini
2021-07-15 19:02   ` Arnaldo Carvalho de Melo
2021-07-15 19:07     ` Arnaldo Carvalho de Melo
2021-07-15 19:20     ` Arnaldo Carvalho de Melo
2022-01-05  6:19       ` Krister Johansen
2022-01-06 20:27         ` Arnaldo Carvalho de Melo
2022-01-06 20:34           ` Arnaldo Carvalho de Melo
2021-07-15 19:44     ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 02/20] perf env: fix sibling_dies memory leak Riccardo Mancini
2021-07-15 19:49   ` Arnaldo Carvalho de Melo
2021-07-16 13:13     ` Riccardo Mancini
2021-07-15 16:07 ` [PATCH 03/20] perf test: session_topology: delete session->evlist Riccardo Mancini
2021-07-15 19:51   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 04/20] perf test: event_update: fix memory leak of evlist Riccardo Mancini
2021-07-15 19:54   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 05/20] perf test: event_update: fix memory leak of unit Riccardo Mancini
2021-07-15 19:58   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 06/20] perf dso: fix memory leak in dso__new_map Riccardo Mancini
2021-07-15 20:01   ` Arnaldo Carvalho de Melo
2021-07-15 20:07   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 07/20] perf test: maps__merge_in: fix memory leak of maps Riccardo Mancini
2021-07-15 20:10   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 08/20] perf env: fix memory leak of cpu_pmu_caps Riccardo Mancini
2021-07-15 20:12   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` Riccardo Mancini [this message]
2021-07-15 20:15   ` [PATCH 09/20] perf report: free generated help strings for sort option Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 10/20] perf inject: close inject.output Riccardo Mancini
2021-07-15 20:17   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 11/20] perf session: cleanup trace_event Riccardo Mancini
2021-07-15 20:21   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 12/20] perf script: release zstd data Riccardo Mancini
2021-07-15 20:23   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 13/20] perf script: fix memory leaks in perf_script Riccardo Mancini
2021-07-15 20:25   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 14/20] perf util/lzma: close lzma stream Riccardo Mancini
2021-07-15 20:31   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 15/20] perf trace: free malloc'd trace fields on exit Riccardo Mancini
2021-07-15 20:32   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 16/20] perf trace: free syscall->arg_fmt Riccardo Mancini
2021-07-15 20:35   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 17/20] perf trace: free syscall tp fields in evsel->priv Riccardo Mancini
2021-07-15 20:35   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 18/20] perf trace: free strings in trace__parse_events_option Riccardo Mancini
2021-07-15 20:36   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 19/20] perf test: bpf: free obj_buf Riccardo Mancini
2021-07-15 20:39   ` Arnaldo Carvalho de Melo
2021-07-16 16:50   ` Arnaldo Carvalho de Melo
2021-07-15 16:07 ` [PATCH 20/20] perf util/probe-file: delete namelist on error in del_events Riccardo Mancini
2021-07-15 20:41   ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a38b13f02812a8a6759200b9063c6191337f44d4.1626343282.git.rickyman7@gmail.com \
    --to=rickyman7@gmail.com \
    --cc=acme@kernel.org \
    --cc=irogers@google.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).