linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: tongtiangen <tongtiangen@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
	<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
	<jolsa@redhat.com>, <namhyung@kernel.org>,
	<kan.liang@linux.intel.com>, <mhiramat@kernel.org>,
	<tongtiangen@huawei.com>, <weiyongjun1@huawei.com>
Cc: <linux-kernel@vger.kernel.org>
Subject: [PATCH] perf header: Fix possible memory leak when using do_read_string
Date: Thu, 2 Jul 2020 18:35:04 +0800	[thread overview]
Message-ID: <20200702103504.119845-1-tongtiangen@huawei.com> (raw)

In the header.c file, some functions allocate memory after using
do_read_string, but the corresponding memory is not released after
subsequent processing errors, causing memory leaks.

Fixes: acae8b36cded ("perf header: Add die information in CPU topology")
Fixes: c60da22aca87 ("perf header: Transform nodes string info to struct")
Fixes: 642aadaa320b ("perf header: Make topology checkers to check return value of strbuf")
Signed-off-by: tongtiangen <tongtiangen@huawei.com>
---
 tools/perf/util/header.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 7a67d017d72c..2f77391e0787 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2307,8 +2307,10 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
 			goto error;
 
 		/* include a NULL character at the end */
-		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
+		if (strbuf_add(&sb, str, strlen(str) + 1) < 0) {
+			free(str);
 			goto error;
+		}
 		size += string_size(str);
 		free(str);
 	}
@@ -2326,8 +2328,10 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
 			goto error;
 
 		/* include a NULL character at the end */
-		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
+		if (strbuf_add(&sb, str, strlen(str) + 1) < 0) {
+			free(str);
 			goto error;
+		}
 		size += string_size(str);
 		free(str);
 	}
@@ -2390,8 +2394,10 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
 			goto error;
 
 		/* include a NULL character at the end */
-		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
+		if (strbuf_add(&sb, str, strlen(str) + 1) < 0) {
+			free(str);
 			goto error;
+		}
 		size += string_size(str);
 		free(str);
 	}
@@ -2446,7 +2452,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
 
 		n->map = perf_cpu_map__new(str);
 		if (!n->map)
-			goto error;
+			goto free_str;
 
 		free(str);
 	}
@@ -2454,6 +2460,8 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
 	ff->ph->env.numa_nodes = nodes;
 	return 0;
 
+free_str:
+	free(str);
 error:
 	free(nodes);
 	return -1;
@@ -2487,10 +2495,10 @@ static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
 			goto error;
 
 		if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
-			goto error;
+			goto free_name;
 		/* include a NULL character at the end */
 		if (strbuf_add(&sb, "", 1) < 0)
-			goto error;
+			goto free_name;
 
 		if (!strcmp(name, "msr"))
 			ff->ph->env.msr_pmu_type = type;
@@ -2501,6 +2509,8 @@ static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
 	ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
 	return 0;
 
+free_name:
+	free(name);
 error:
 	strbuf_release(&sb);
 	return -1;
-- 
2.20.1


             reply	other threads:[~2020-07-02 10:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-02 10:35 tongtiangen [this message]
2020-07-02 15:07 [PATCH] perf header: Fix possible memory leak when using do_read_string Markus Elfring

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=20200702103504.119845-1-tongtiangen@huawei.com \
    --to=tongtiangen@huawei.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@redhat.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=weiyongjun1@huawei.com \
    /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).