[PATCH v1 4/8] perf evsel: Avoid container_of on a NULL leader

From: Ian Rogers
Date: Fri Dec 13 2024 - 16:05:59 EST


An evsel should typically have a leader of itself, however, in tests
like 'Sample parsing' a NULL leader may occur and the container_of
will return a corrupt pointer. Avoid this with an explicit NULL test.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
Fixes: fba7c86601e2 ("libperf: Move 'leader' from tools/perf to perf_evsel::leader")
---
tools/perf/util/evsel.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index cb6952b6d9d9..dc427eafca6c 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3327,6 +3327,8 @@ bool evsel__is_hybrid(const struct evsel *evsel)

struct evsel *evsel__leader(const struct evsel *evsel)
{
+ if (evsel->core.leader == NULL)
+ return NULL;
return container_of(evsel->core.leader, struct evsel, core);
}

--
2.47.1.613.gc27f4b7a9f-goog