Re: [PATCH v2 4/5] math64: Silence a clang -Wshorten-64-to-32 warning

From: David Laight
Date: Fri May 02 2025 - 08:18:21 EST


On Thu, 1 May 2025 14:11:59 -0700
Ian Rogers <irogers@xxxxxxxxxx> wrote:

....
> Sorry I don't understand what you're saying. Java certainly has bugs
> in this area which is why I've written checkers like:
> https://errorprone.info/bugpattern/BadComparable
> For code similar to:
> ```
> s32 compare(s64 a, s64 b) { return (s32)(a - b); }
> ```
> where the truncation is going to throw away the sign of the subtract
> and is almost certainly a bug. This matches the bugs that are fixed in
> this patch series for the perf code, in particular an issue on ARM
> that Leo Yan originally provided the fix for:
> https://lore.kernel.org/lkml/20250331172759.115604-1-leo.yan@xxxxxxx/

That code is wrong with or without the (s32) cast.
And the explicit cast will hide the compiler warning.

If you want the compiler to find bugs you need to reduce the number
of casts to an absolute minimum and disable/fix the compiler warning
for false positives.

These type based (rather than value domain) warnings are all a PITA.

Another example is the 'signed v unsigned compare' which bleats for:
int rval = read(... sizeof (foo));
if (rval < 0)
return -1;
if (rval != sizeof (foo))
// truncated
Whereas a statically_true(rval >= 0) test will pass.

David