[PATCH next v2 1/2] perf tools: fix potential memleak in perf events parser

From: Chen Wandun
Date: Thu Jun 11 2020 - 10:56:56 EST


From: Cheng Jian <cj.chengjian@xxxxxxxxxx>

Fix memory leak of in function parse_events_term__sym_hw()
and parse_events_term__clone() when error occur.

Fixes: b6645a723595 ("perf parse: Ensure config and str in terms are unique")
Signed-off-by: Cheng Jian <cj.chengjian@xxxxxxxxxx>
Signed-off-by: Chen Wandun <chenwandun@xxxxxxxxxx>
---
tools/perf/util/parse-events.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 3decbb203846..6f4dc8a92817 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2957,8 +2957,12 @@ int parse_events_term__sym_hw(struct parse_events_term **term,
sym = &event_symbols_hw[idx];

str = strdup(sym->symbol);
- if (!str)
+ if (!str) {
+ if (!config)
+ free(temp.config);
return -ENOMEM;
+ }
+
return new_term(term, &temp, str, 0);
}

@@ -2983,8 +2987,12 @@ int parse_events_term__clone(struct parse_events_term **new,
return new_term(new, &temp, NULL, term->val.num);

str = strdup(term->val.str);
- if (!str)
+ if (!str) {
+ if (term->config)
+ free(temp.config);
return -ENOMEM;
+ }
+
return new_term(new, &temp, str, 0);
}

--
2.17.1