Skip to content

Commit a0a4173

Browse files
captain5050namhyung
authored andcommitted
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>
1 parent 6744c0b commit a0a4173

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

tools/lib/perf/cpumap.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
368368
.cpu = -1
369369
};
370370

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);
375377
}
376378

377379
/** Is 'b' a subset of 'a'. */

0 commit comments

Comments
 (0)