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

libperf xyarray: Add bounds checks to xyarray__entry()

xyarray__entry() is missing any bounds checking yet often the x and y
parameters come from external callers. Add bounds checks and an
unchecked __xyarray__entry().

Committer notes:

Make the 'x' and 'y' arguments to the new xyarray__entry() that does
bounds check to be of type 'size_t', so that we cover also the case
where 'x' and 'y' could be negative, which is needed anyway as having
them as 'int' breaks the build with:

/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h: In function ‘xyarray__entry’:
/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:8: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
28 | if (x >= xy->max_x || y >= xy->max_y)
| ^~
/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:26: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
28 | if (x >= xy->max_x || y >= xy->max_y)
| ^~
cc1: all warnings being treated as errors

Signed-off-by: Rob Herring <robh@kernel.org>
Suggested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20210414195758.4078803-1-robh@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Rob Herring and committed by
Arnaldo Carvalho de Melo
81886948 47d01e7b

+8 -1
+8 -1
tools/lib/perf/include/internal/xyarray.h
··· 18 18 void xyarray__delete(struct xyarray *xy); 19 19 void xyarray__reset(struct xyarray *xy); 20 20 21 - static inline void *xyarray__entry(struct xyarray *xy, int x, int y) 21 + static inline void *__xyarray__entry(struct xyarray *xy, int x, int y) 22 22 { 23 23 return &xy->contents[x * xy->row_size + y * xy->entry_size]; 24 + } 25 + 26 + static inline void *xyarray__entry(struct xyarray *xy, size_t x, size_t y) 27 + { 28 + if (x >= xy->max_x || y >= xy->max_y) 29 + return NULL; 30 + return __xyarray__entry(xy, x, y); 24 31 } 25 32 26 33 static inline int xyarray__max_y(struct xyarray *xy)