[PATCH 5.8 060/118] perf metric: Fix some memory leaks

From: Greg Kroah-Hartman
Date: Mon Sep 21 2020 - 12:54:28 EST

From: Namhyung Kim <namhyung@xxxxxxxxxx>

[ Upstream commit 4f57a1ed749a81ec553d89233cab53db9365e193 ]

I found some memory leaks while reading the metric code. Some are real
and others only occur in the error path. When it failed during metric
or event parsing, it should release all resources properly.

Fixes: b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
Acked-by: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
Cc: Andi Kleen <ak@xxxxxxxxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: John Garry <john.garry@xxxxxxxxxx>
Cc: Kajol Jain <kjain@xxxxxxxxxxxxx>
Cc: Mark Rutland <mark.rutland@xxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Stephane Eranian <eranian@xxxxxxxxxx>
Link: http://lore.kernel.org/lkml/20200915031819.386559-2-namhyung@xxxxxxxxxx
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
tools/perf/util/metricgroup.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 9e21aa767e417..344a75718afc3 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -443,6 +443,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
strlist__add(me->metrics, s);
+ if (!raw)
+ free(s);
@@ -726,7 +729,7 @@ int metricgroup__parse_groups(const struct option *opt,
ret = metricgroup__add_metric_list(str, metric_no_group,
&extra_events, &group_list);
if (ret)
- return ret;
+ goto out;
pr_debug("adding %s\n", extra_events.buf);
bzero(&parse_error, sizeof(parse_error));
ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
@@ -734,11 +737,11 @@ int metricgroup__parse_groups(const struct option *opt,
parse_events_print_error(&parse_error, extra_events.buf);
goto out;
- strbuf_release(&extra_events);
ret = metricgroup__setup_events(&group_list, metric_no_merge,
perf_evlist, metric_events);
+ strbuf_release(&extra_events);
return ret;