RE: [PATCH 0/1] Slightly relax the type checking done by min() and max().

From: David Laight
Date: Fri Nov 25 2022 - 10:31:36 EST


From: Andy Shevchenko
> Sent: 25 November 2022 15:21
>
> On Fri, Nov 25, 2022 at 03:00:40PM +0000, David Laight wrote:
> > The min() and max() defines include a type check to avoid the unexpected
> > behaviour when a negative value is compared against and unsigned value.
> > However a lot of code hits this check and uses min_t() to avoid the error.
> > Many of these are just plain wrong.
> >
> > Those casting to u8 or u16 are particularly suspect, eg:
> > drivers/usb/misc/usb251xb.c:528:
> > hub->max_current_sp = min_t(u8, property_u32 / 2000, 50);
>
> I don't buy this. What's exactly wrong with this code?

Consider what happens if propery_u32 is 512000.
The returned value is 0 not 50.

David

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