]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf tools: Fix get_max_num() size_t underflow on empty sysfs file
authorArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 6 Jun 2026 23:37:52 +0000 (20:37 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 10 Jun 2026 14:38:45 +0000 (11:38 -0300)
get_max_num() reads a sysfs file (cpu/possible, cpu/present, or
node/possible) and scans backward from the end to find the last
number.  If the file is empty, filename__read_str() returns num == 0.
The loop `while (--num)` decrements the size_t from 0 to SIZE_MAX,
reading backward across the heap until a comma or hyphen is found
or unmapped memory is hit.

Add an early return for empty files before the backward scan.

Fixes: 7780c25bae59fd04 ("perf tools: Allow ability to map cpus to nodes easily")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Ian Rogers <irogers@google.com>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/cpumap.c

index 21fa781b03cc74097fdc49db92f132a71f114dc6..1fab00ec4a59a0c719a170be7413a7f3cf1c2cc8 100644 (file)
@@ -448,6 +448,12 @@ static int get_max_num(char *path, int *max)
 
        buf[num] = '\0';
 
+       /* empty file — nothing to parse */
+       if (num == 0) {
+               err = -1;
+               goto out;
+       }
+
        /* start on the right, to find highest node num */
        while (--num) {
                if ((buf[num] == ',') || (buf[num] == '-')) {