From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric()
Date: Tue, 17 Nov 2026 23:55:14 -0800 [thread overview]
Message-ID: <20261118075516.1278903-4-irogers@google.com> (raw)
In-Reply-To: <20261118075516.1278903-1-irogers@google.com>
In resolve_metric(), growing the pending array assigns the result of
realloc() directly back to pending; if realloc() fails after earlier
iterations succeeded, the existing pending allocation is overwritten
with NULL and leaked.
Similarly, in __add_metric(), assigning realloc() directly to
root_metric->metric_refs overwrites the existing pointer with NULL on
failure, preventing metric__free(root_metric) in the root caller from
freeing the prior metric_refs array.
Use temporary pointers for both realloc() calls, freeing pending before
returning -ENOMEM in resolve_metric() and preserving
root_metric->metric_refs for metric__free() in __add_metric().
Fixes: 80be6434c36f ("perf metric: Modify resolution and recursion check")
Fixes: 46bdc0bf8d21 ("perf metric: Simplify metric_refs calculation")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/metricgroup.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 779fe56..489f13b 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -771,10 +771,15 @@ static int resolve_metric(struct list_head *metric_list,
if (pmu_metrics_table__find_metric(table, pmu, cur->pkey,
metricgroup__find_metric_callback,
&pm) != PMU_METRICS__NOT_FOUND) {
- pending = realloc(pending,
- (pending_cnt + 1) * sizeof(struct to_resolve));
- if (!pending)
+ struct to_resolve *new_pending;
+
+ new_pending = realloc(pending,
+ (pending_cnt + 1) * sizeof(struct to_resolve));
+ if (!new_pending) {
+ free(pending);
return -ENOMEM;
+ }
+ pending = new_pending;
memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm));
pending[pending_cnt].key = cur->pkey;
@@ -863,6 +868,7 @@ static int __add_metric(struct list_head *metric_list,
return -ENOMEM;
} else {
+ struct metric_ref *new_refs;
int cnt = 0;
/*
@@ -879,10 +885,11 @@ static int __add_metric(struct list_head *metric_list,
}
/* Create reference. Need space for the entry and the terminator. */
- root_metric->metric_refs = realloc(root_metric->metric_refs,
- (cnt + 2) * sizeof(struct metric_ref));
- if (!root_metric->metric_refs)
+ new_refs = realloc(root_metric->metric_refs,
+ (cnt + 2) * sizeof(struct metric_ref));
+ if (!new_refs)
return -ENOMEM;
+ root_metric->metric_refs = new_refs;
/*
* Intentionally passing just const char pointers,
--
2.56.1.703.gf848ed807c-goog
next prev parent reply other threads:[~2026-11-18 7:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 7:55 [PATCH v1 0/4] perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks Ian Rogers
2026-11-18 7:55 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
2026-11-18 7:55 ` [PATCH v1 2/4] perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs Ian Rogers
2026-11-18 7:55 ` Ian Rogers [this message]
2026-11-18 7:55 ` [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias() Ian Rogers
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=20261118075516.1278903-4-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--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
all inboxes | Powered by JetHome®