[PATCH] perf metric: Fix some memory leaks

From: Namhyung Kim
Date: Thu Sep 03 2020 - 23:21:59 EST


I found some memory leaks while reading the metric code. Some are
real and others only occur in the error path.

Cc: Kajol Jain <kjain@xxxxxxxxxxxxx>
Cc: John Garry <john.garry@xxxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Fixes: 9afe5658a6fa8 ("perf tools: Release metric_events rblist")
Fixes: 4ea2896715e67 ("perf metric: Collect referenced metrics in struct metric_expr")
Fixes: 71b0acce78d12 ("perf list: Add metric groups to perf list")
Fixes: b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
I'm not sure it'd better have them together or split each fix as
they came from different commits. Please let me know if you prefer
split.

tools/perf/util/metricgroup.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 8831b964288f..7e31c4578ce8 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -85,6 +85,7 @@ static void metric_event_delete(struct rblist *rblist __maybe_unused,

list_for_each_entry_safe(expr, tmp, &me->head, nd) {
free(expr->metric_refs);
+ free(expr->metric_events);
free(expr);
}

@@ -316,6 +317,7 @@ static int metricgroup__setup_events(struct list_head *groups,
if (!metric_refs) {
ret = -ENOMEM;
free(metric_events);
+ free(expr);
break;
}

@@ -530,6 +532,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
continue;
strlist__add(me->metrics, s);
}
+
+ if (!raw)
+ free(s);
}
free(omg);
}
@@ -1048,11 +1053,11 @@ static int parse_groups(struct evlist *perf_evlist, const char *str,
parse_events_print_error(&parse_error, extra_events.buf);
goto out;
}
- strbuf_release(&extra_events);
ret = metricgroup__setup_events(&metric_list, metric_no_merge,
perf_evlist, metric_events);
out:
metricgroup__free_metrics(&metric_list);
+ strbuf_release(&extra_events);
return ret;
}

--
2.28.0.526.ge36021eeef-goog