From: Ian Rogers Date: Wed, 3 Dec 2025 21:47:01 +0000 (-0800) Subject: libperf cpumap: Fix perf_cpu_map__max for an empty/NULL map X-Git-Tag: v6.19-rc1~61^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0a4173631bfcfd3520192c0a61cf911d6a52c3a;p=thirdparty%2Fkernel%2Flinux.git libperf cpumap: Fix perf_cpu_map__max for an empty/NULL map Passing an empty map to perf_cpu_map__max triggered a SEGV. Explicitly test for the empty map. Reported-by: Ingo Molnar Closes: https://lore.kernel.org/linux-perf-users/aSwt7yzFjVJCEmVp@gmail.com/ Tested-by: Ingo Molnar Signed-off-by: Ian Rogers Tested-by: Thomas Richter Signed-off-by: Namhyung Kim --- diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index 7e88417ba84d1..4160e7d2e120f 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -368,10 +368,12 @@ struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map) .cpu = -1 }; - // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well. - return __perf_cpu_map__nr(map) > 0 - ? __perf_cpu_map__cpu(map, __perf_cpu_map__nr(map) - 1) - : result; + if (!map) + return result; + + // The CPUs are always sorted and nr is always > 0 as 0 length map is + // encoded as NULL. + return __perf_cpu_map__cpu(map, __perf_cpu_map__nr(map) - 1); } /** Is 'b' a subset of 'a'. */