* lib/xsize.h (xtimes): Instead of casting N to size_t, widen it to size_t if it isn’t already wide enough. --- ChangeLog | 4 ++++ lib/xsize.h | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog index 94845b5ede..3f123f2071 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2026-05-07 Paul Eggert <[email protected]> + xsize: pacify -Wuseless-cast by ‘1 *’ + * lib/xsize.h (xtimes): Instead of casting N to size_t, + widen it to size_t if it isn’t already wide enough. + Pacify -Wuseless cast with a pragma * lib/intprops-internal.h: Ignore -Wuseless-cast. * lib/vasnprintf.c: Ignore -Wuseless-cast. diff --git a/lib/xsize.h b/lib/xsize.h index b76cf49fdf..a54cc179aa 100644 --- a/lib/xsize.h +++ b/lib/xsize.h @@ -108,10 +108,12 @@ xmax (size_t size1, size_t size2) The count must be >= 0 and the element size must be > 0. Arguments should not have side effects. The element size's type should be no wider than size_t. + The 'sizeof "" *' widens N's value if necessary, to avoid overflow; + unlike a cast to size_t, this pacifies -Wuseless-cast. This is a macro, not a function, so that it works correctly even when N is of a wider type and N > SIZE_MAX. */ #define xtimes(N, ELSIZE) \ - ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX) + ((N) <= SIZE_MAX / (ELSIZE) ? sizeof "" * (N) * (ELSIZE) : SIZE_MAX) /* Check for overflow. */ #define size_overflow_p(SIZE) \ -- 2.54.0
