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

From: Ian Rogers

Date: Mon Jun 15 2026 - 02:23:36 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 | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 38a41bd6c9b5..a6593c388a48 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2089,10 +2089,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;
@@ -3338,8 +3338,10 @@ 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);
@@ -3372,8 +3374,10 @@ 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