Patrick Williams | e760df8 | 2023-05-26 11:10:49 -0500 | [diff] [blame] | 1 | From d14450f9e0f05ea7177c5404a7a9289352caab77 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Mon, 23 Jan 2023 13:04:10 -0800 |
| 4 | Subject: [PATCH] perf cpumap: Make counter as unsigned ints |
| 5 | |
| 6 | These are loop counters which is inherently unsigned. Therefore make |
| 7 | them unsigned. Moreover it also fixes alloc-size-larger-than |
| 8 | error with gcc-13, where malloc can be called with (-1) due to tmp_len |
| 9 | being an int type. |
| 10 | |
| 11 | Fixes |
| 12 | | cpumap.c:366:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] |
| 13 | | 366 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); |
| 14 | | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 15 | |
| 16 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 17 | Cc: Peter Zijlstra <peterz@infradead.org> |
| 18 | Cc: Ingo Molnar <mingo@redhat.com> |
| 19 | Cc: Arnaldo Carvalho de Melo <acme@kernel.org> |
| 20 | Cc: Mark Rutland <mark.rutland@arm.com> |
| 21 | Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> |
| 22 | Cc: Jiri Olsa <jolsa@kernel.org> |
| 23 | Cc: Namhyung Kim <namhyung@kernel.org> |
| 24 | |
| 25 | Upstream-Status: Submitted [https://lore.kernel.org/linux-perf-users/20230123211310.127532-1-raj.khem@gmail.com/T/#u] |
| 26 | --- |
| 27 | tools/lib/perf/cpumap.c | 10 +++++----- |
| 28 | 1 file changed, 5 insertions(+), 5 deletions(-) |
| 29 | |
| 30 | diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c |
| 31 | index 6cd0be7c1bb4..d960880dd903 100644 |
| 32 | --- a/tools/lib/perf/cpumap.c |
| 33 | +++ b/tools/lib/perf/cpumap.c |
| 34 | @@ -351,8 +351,8 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, |
| 35 | struct perf_cpu_map *other) |
| 36 | { |
| 37 | struct perf_cpu *tmp_cpus; |
| 38 | - int tmp_len; |
| 39 | - int i, j, k; |
| 40 | + unsigned int tmp_len; |
| 41 | + unsigned int i, j, k; |
| 42 | struct perf_cpu_map *merged; |
| 43 | |
| 44 | if (perf_cpu_map__is_subset(orig, other)) |
| 45 | @@ -369,7 +369,7 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, |
| 46 | |
| 47 | /* Standard merge algorithm from wikipedia */ |
| 48 | i = j = k = 0; |
| 49 | - while (i < orig->nr && j < other->nr) { |
| 50 | + while (i < (unsigned int)orig->nr && j < (unsigned int)other->nr) { |
| 51 | if (orig->map[i].cpu <= other->map[j].cpu) { |
| 52 | if (orig->map[i].cpu == other->map[j].cpu) |
| 53 | j++; |
| 54 | @@ -378,10 +378,10 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, |
| 55 | tmp_cpus[k++] = other->map[j++]; |
| 56 | } |
| 57 | |
| 58 | - while (i < orig->nr) |
| 59 | + while (i < (unsigned int)orig->nr) |
| 60 | tmp_cpus[k++] = orig->map[i++]; |
| 61 | |
| 62 | - while (j < other->nr) |
| 63 | + while (j < (unsigned int)other->nr) |
| 64 | tmp_cpus[k++] = other->map[j++]; |
| 65 | assert(k <= tmp_len); |
| 66 | |
| 67 | -- |
| 68 | 2.39.1 |
| 69 | |