[PATCH 6.18 005/227] perf parse-events: Fix evsel allocation failure

Greg Kroah-Hartman
2026-01-28T15:51:46Z

From: Faisal Bukhari <faisalbukhari523@gmail.com>

[ Upstream commit 1eb217ab2e737609f8a861b517649e82e7236d05 ]

If evsel__new_idx() returns NULL, the function currently jumps to label
'out_err'. Here, references to `cpus` and `pmu_cpus` are dropped.
Also, resources held by evsel->name and evsel->metric_id are freed.

But if evsel__new_idx() returns NULL, it can lead to NULL pointer
dereference.

Fixes: cd63c22168257a0b ("perf parse-events: Minor __add_event refactoring")
Signed-off-by: Faisal Bukhari <faisalbukhari523@gmail.com>
Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
tools/perf/util/parse-events.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index cd9315d3ca117..4723c2955f22e 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -286,8 +286,11 @@ __add_event(struct list_head *list, int *idx,
event_attr_init(attr);

evsel = evsel__new_idx(attr, *idx);
- if (!evsel)
- goto out_err;
+ if (!evsel) {
+ perf_cpu_map__put(cpus);
+ perf_cpu_map__put(pmu_cpus);
+ return NULL;
+ }

if (name) {
evsel->name = strdup(name);
--
2.51.0