public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chen Wandun <chenwandun@huawei.com>
To: <acme@kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <peterz@infradead.org>, <mingo@redhat.com>,
	<Markus.Elfring@web.de>, <cj.chengjian@huawei.com>,
	<chenwandun@huawei.com>
Subject: [PATCH next v2 2/2] perf tools: fix potential memleak in perf events parser
Date: Thu, 11 Jun 2020 22:56:05 +0800	[thread overview]
Message-ID: <20200611145605.21427-3-chenwandun@huawei.com> (raw)
In-Reply-To: <20200611145605.21427-1-chenwandun@huawei.com>

Fix potential memory leak. Function new_term may return error, so
it is need to free memory when the return value is negative.

Fixes: b6645a723595 ("perf parse: Ensure config and str in terms are unique")

Signed-off-by: Chen Wandun <chenwandun@huawei.com>
---
 tools/perf/util/parse-events.c | 47 ++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 10 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 6f4dc8a92817..a9f32032af08 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2947,6 +2947,7 @@ int parse_events_term__sym_hw(struct parse_events_term **term,
 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
 		.config    = config,
 	};
+	int ret;
 
 	if (!temp.config) {
 		temp.config = strdup("event");
@@ -2958,12 +2959,23 @@ int parse_events_term__sym_hw(struct parse_events_term **term,
 
 	str = strdup(sym->symbol);
 	if (!str) {
-		if (!config)
-			free(temp.config);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto free_config;
 	}
 
-	return new_term(term, &temp, str, 0);
+	ret = new_term(term, &temp, str, 0);
+	if (ret)
+		goto free_str;
+
+	return 0;
+
+free_str:
+	free(str);
+free_config:
+	if (!config)
+		free(temp.config);
+
+	return ret;
 }
 
 int parse_events_term__clone(struct parse_events_term **new,
@@ -2977,23 +2989,38 @@ int parse_events_term__clone(struct parse_events_term **new,
 		.err_term  = term->err_term,
 		.err_val   = term->err_val,
 	};
+	int ret;
 
 	if (term->config) {
 		temp.config = strdup(term->config);
 		if (!temp.config)
 			return -ENOMEM;
 	}
-	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
-		return new_term(new, &temp, NULL, term->val.num);
+	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
+		ret = new_term(new, &temp, NULL, term->val.num);
+		if (ret)
+			goto free_config;
+	}
 
 	str = strdup(term->val.str);
 	if (!str) {
-		if (term->config)
-			free(temp.config);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto free_config;
 	}
 
-	return new_term(new, &temp, str, 0);
+	ret = new_term(new, &temp, str, 0);
+	if (ret)
+		goto free_str;
+
+	return 0;
+
+free_str:
+	free(str);
+free_config:
+	if (term->config)
+		free(temp.config);
+
+	return ret;
 }
 
 void parse_events_term__delete(struct parse_events_term *term)
-- 
2.17.1


  parent reply	other threads:[~2020-06-11 14:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-11 14:56 [PATCH next v2 0/2] fix potential memleak in perf events parser Chen Wandun
2020-06-11 14:56 ` [PATCH next v2 1/2] perf tools: " Chen Wandun
2020-06-11 18:50   ` [PATCH v2 1/2] perf tools: Fix potential memory leaks " Markus Elfring
2020-06-12 10:01     ` Greg KH
2020-06-14  3:39     ` Chen Wandun
2020-06-11 14:56 ` Chen Wandun [this message]
2020-06-11 19:09   ` [PATCH v2 2/2] perf tools: Improve exception handling in two functions of " 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=20200611145605.21427-3-chenwandun@huawei.com \
    --to=chenwandun@huawei.com \
    --cc=Markus.Elfring@web.de \
    --cc=acme@kernel.org \
    --cc=cj.chengjian@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --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