[PATCH v3 1/7] libperf threadmap: Don't segv for index 0 for the NULL perf_thread_map
From: Ian Rogers
Date: Mon May 19 2025 - 15:52:28 EST
perf_thread_map__nr returns length 1 if the perf_thread_map is NULL,
meaning index 0 is valid. When perf_thread_map__pid of index 0 is read
then return the expected "any" -1 value. Assert this is only done for
index 0.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/lib/perf/threadmap.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/lib/perf/threadmap.c b/tools/lib/perf/threadmap.c
index 07968f3ea093..3ca9ba4987fc 100644
--- a/tools/lib/perf/threadmap.c
+++ b/tools/lib/perf/threadmap.c
@@ -97,5 +97,10 @@ int perf_thread_map__nr(struct perf_thread_map *threads)
pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx)
{
+ if (!map) {
+ assert(idx == 0);
+ return -1;
+ }
+
return map->map[idx].pid;
}
--
2.49.0.1101.gccaa498523-goog