]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
libperf cpumap: Fix perf_cpu_map__max for an empty/NULL map
authorIan Rogers <irogers@google.com>
Wed, 3 Dec 2025 21:47:01 +0000 (13:47 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 Jan 2026 11:57:02 +0000 (12:57 +0100)
[ Upstream commit a0a4173631bfcfd3520192c0a61cf911d6a52c3a ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/lib/perf/cpumap.c

index b20a5280f2b333d46db72821d2ef30e9e97787bb..2bbbe1c782b8a4f756cbd1686defff046748e434 100644 (file)
@@ -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'. */