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

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 <mingo@kernel.org>
Closes: https://lore.kernel.org/linux-perf-users/aSwt7yzFjVJCEmVp@gmail.com/
Tested-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
a0a41736 6744c0b1

+6 -4
+6 -4
tools/lib/perf/cpumap.c
··· 368 368 .cpu = -1 369 369 }; 370 370 371 - // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well. 372 - return __perf_cpu_map__nr(map) > 0 373 - ? __perf_cpu_map__cpu(map, __perf_cpu_map__nr(map) - 1) 374 - : result; 371 + if (!map) 372 + return result; 373 + 374 + // The CPUs are always sorted and nr is always > 0 as 0 length map is 375 + // encoded as NULL. 376 + return __perf_cpu_map__cpu(map, __perf_cpu_map__nr(map) - 1); 375 377 } 376 378 377 379 /** Is 'b' a subset of 'a'. */