[PATCH v17 18/20] perf python: Handle Py_None for thread and cpu maps

From: Ian Rogers

Date: Sat Jun 13 2026 - 03:17:41 EST


The python stubs allow passing None for threads and cpus to the
perf.parse_events() and perf.parse_metrics() bindings. However, PyArg_ParseTuple
parses None into a Py_None object, which is not a NULL pointer.
Because the C code lacked an explicit check for Py_None, it would cast
Py_None to a pyrf_thread_map/pyrf_cpu_map struct pointer and dereference it,
causing a memory corruption crash.

Fix this pre-existing issue by explicitly checking for Py_None alongside NULL
in pyrf__parse_events, pyrf__parse_metrics, and pyrf_evsel__open.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/python.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index ee88b3af98e3..8d4877148aa6 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2086,10 +2086,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
&pcpus, &pthreads, &group, &inherit))
return NULL;

- if (pthreads != NULL)
+ if (pthreads != NULL && pthreads != Py_None)
threads = ((struct pyrf_thread_map *)pthreads)->threads;

- if (pcpus != NULL)
+ if (pcpus != NULL && pcpus != Py_None)
cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;

evsel->core.attr.inherit = inherit;
@@ -3335,8 +3335,8 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)
return NULL;
}

- threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
- cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
+ threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
+ cpus = (pcpus && pcpus != Py_None) ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;

parse_events_error__init(&err);
perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
@@ -3369,8 +3369,8 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args)
return NULL;
}

- threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
- cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
+ threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
+ cpus = (pcpus && pcpus != Py_None) ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;

perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
ret = metricgroup__parse_groups(evlist, pmu ?: "all", input,
--
2.54.0.1136.gdb2ca164c4-goog