RE: arm64: include/linux/compiler_types.h:542:38: error: call to '__compiletime_assert_1050' declared with attribute error: clamp() low limit min greater than high limit max_avail

From: David Laight
Date: Thu Dec 05 2024 - 21:20:02 EST


From: Naresh Kamboju
> Sent: 05 December 2024 18:42
>
> On Thu, 5 Dec 2024 at 20:46, Dan Carpenter <dan.carpenter@xxxxxxxxxx> wrote:
> >
> > Add David to the CC list.
>
> Anders bisected this reported issue and found the first bad commit as,
>
> # first bad commit:
> [ef32b92ac605ba1b7692827330b9c60259f0af49]
> minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp()

That 'just' changed the test to use __builtin_constant_p() and
thus gets checked after the optimiser has run.

I can paraphrase the code as:
unsigned int fn(unsigned int x)
{
return clamp(10, 5, x == 0 ? 0 : x - 1);
}
which is never actually called with x <= 5.
The compiler converts it to:
return x < 0 ? clamp(10, 5, 0) : clamp(10, 5, x);
(Probably because it can see that clamp(10, 5, 0) is constant.)
And then the compile-time sanity check in clamp() fires.

The order of the arguments to clamp is just wrong!

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)