Skip to contentSkip to conversation

perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events

PATCH In review Wang Yan 2 participants 2 messages Jul 10 – Jul 14 perf events

lore
Share & tools
lore

Wang Yan

author
#0 lore raw

PATCH 1/1 perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events

In metricgroup__copy_metric_events(), new_expr->metric_name is allocated via strdup() but is not freed in all error paths, leading to a memory leak when subsequent allocations or evsel lookups fail.

Add the missing zfree() calls:

- On strdup() failure, free the already allocated new_expr.
- On metric_refs allocation failure, free metric_name before freeing new_expr.
- On metric_events allocation failure, free metric_name in addition to existing freeing of metric_refs and new_expr.
- On evsel lookup failure, free metric_name along with the other resources already freed.

Fixes b214ba8c4275 ("perf tools: Copy metric events properly when expand cgroups")
---
 tools/perf/util/metricgroup.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/metricgroup.c +6 -1
index 69bfa2a723b2..96dd780a5601 100644
@@ -1693,8 +1693,10 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 			new_expr->metric_expr = old_expr->metric_expr;
 			new_expr->metric_threshold = old_expr->metric_threshold;
 			new_expr->metric_name = strdup(old_expr->metric_name);
-			if (!new_expr->metric_name)
+			if (!new_expr->metric_name) {
+				free(new_expr);
 				return -ENOMEM;
+			}
 
 			new_expr->metric_unit = old_expr->metric_unit;
 			new_expr->runtime = old_expr->runtime;
@@ -1707,6 +1709,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 				alloc_size = sizeof(*new_expr->metric_refs);
 				new_expr->metric_refs = calloc(nr + 1, alloc_size);
 				if (!new_expr->metric_refs) {
+					zfree(&new_expr->metric_name);
 					free(new_expr);
 					return -ENOMEM;
 				}
@@ -1724,6 +1727,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 			new_expr->metric_events = calloc(nr + 1, alloc_size);
 			if (!new_expr->metric_events) {
 				zfree(&new_expr->metric_refs);
+				zfree(&new_expr->metric_name);
 				free(new_expr);
 				return -ENOMEM;
 			}
@@ -1735,6 +1739,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 				if (evsel == NULL) {
 					zfree(&new_expr->metric_events);
 					zfree(&new_expr->metric_refs);
+					zfree(&new_expr->metric_name);
 					free(new_expr);
 					return -EINVAL;
 				}
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hello,

Quoted 14 lines · Wang Yan

In metricgroup__copy_metric_events(), new_expr->metric_name is allocated via strdup() but is not freed in all error paths, leading to a memory leak when subsequent allocations or evsel lookups fail.

Add the missing zfree() calls:

- On strdup() failure, free the already allocated new_expr.
- On metric_refs allocation failure, free metric_name before freeing new_expr.
- On metric_events allocation failure, free metric_name in addition to existing freeing of metric_refs and new_expr.
- On evsel lookup failure, free metric_name along with the other resources already freed.

Quoted 2 lines
Fixes b214ba8c4275 ("perf tools: Copy metric events properly when expand cgroups")

It's already fixed in

https://lore.kernel.org/r/[email protected]

Thanks,
Namhyung

Quoted 47 lines
---
 tools/perf/util/metricgroup.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/metricgroup.c +6 -1
index 69bfa2a723b2..96dd780a5601 100644
@@ -1693,8 +1693,10 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 			new_expr->metric_expr = old_expr->metric_expr;
 			new_expr->metric_threshold = old_expr->metric_threshold;
 			new_expr->metric_name = strdup(old_expr->metric_name);
-			if (!new_expr->metric_name)
+			if (!new_expr->metric_name) {
+				free(new_expr);
 				return -ENOMEM;
+			}
 
 			new_expr->metric_unit = old_expr->metric_unit;
 			new_expr->runtime = old_expr->runtime;
@@ -1707,6 +1709,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 				alloc_size = sizeof(*new_expr->metric_refs);
 				new_expr->metric_refs = calloc(nr + 1, alloc_size);
 				if (!new_expr->metric_refs) {
+					zfree(&new_expr->metric_name);
 					free(new_expr);
 					return -ENOMEM;
 				}
@@ -1724,6 +1727,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 			new_expr->metric_events = calloc(nr + 1, alloc_size);
 			if (!new_expr->metric_events) {
 				zfree(&new_expr->metric_refs);
+				zfree(&new_expr->metric_name);
 				free(new_expr);
 				return -ENOMEM;
 			}
@@ -1735,6 +1739,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
 				if (evsel == NULL) {
 					zfree(&new_expr->metric_events);
 					zfree(&new_expr->metric_refs);
+					zfree(&new_expr->metric_name);
 					free(new_expr);
 					return -EINVAL;
 				}
Signature · 1 line
2.25.1