Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf python: Set index error for invalid thread/cpu map items

Returning NULL for out of bound CPU or thread map items causes
internal errors. Fix by correctly setting the error to be an index
error.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250710235126.1086011-14-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
b4aff7ed 421c5f39

+6 -2
+6 -2
tools/perf/util/python.c
··· 529 529 { 530 530 struct pyrf_cpu_map *pcpus = (void *)obj; 531 531 532 - if (i >= perf_cpu_map__nr(pcpus->cpus)) 532 + if (i >= perf_cpu_map__nr(pcpus->cpus)) { 533 + PyErr_SetString(PyExc_IndexError, "Index out of range"); 533 534 return NULL; 535 + } 534 536 535 537 return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu); 536 538 } ··· 600 598 { 601 599 struct pyrf_thread_map *pthreads = (void *)obj; 602 600 603 - if (i >= perf_thread_map__nr(pthreads->threads)) 601 + if (i >= perf_thread_map__nr(pthreads->threads)) { 602 + PyErr_SetString(PyExc_IndexError, "Index out of range"); 604 603 return NULL; 604 + } 605 605 606 606 return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, i)); 607 607 }