RE: [PATCH] mm/vmalloc: Replace the ternary conditional operator with min()

From: David Laight
Date: Sat Jun 10 2023 - 18:29:36 EST


From: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> Sent: 10 June 2023 23:08
>
> On Sat, 10 Jun 2023 22:06:35 +0100 Lorenzo Stoakes <lstoakes@xxxxxxxxx> wrote:
>
> > > > OK, as per the pedantic test bot, you'll need to change this to:-
> > > >
> > > > num = min_t(size_t, remains, PAGE_SIZE);
>
> PAGE_SIZE is a nuisance. It _usually_ creates the need for a
> cast:
>
> hp2:/usr/src/linux-6.4-rc4> grep -r "min(.*PAGE_SIZE" . | wc -l
> 117
> hp2:/usr/src/linux-6.4-rc4> grep -r "min_t(.*PAGE_SIZE" . | wc -l
> 279
>
> Perhaps it should always have been size_t.
>
> I suppose we could do
>
> #define PAGE_SIZE_T (size_t)PAGE_SIZE
>
> And use that where needed. Mainly because I like the name ;)

Or someone take my patches to relax the checks min() does a bit.
I think I last posted them in January.
Basically:
- unsigned v unsigned is always ok.
- signed v signed is always ok.
- unsigned v signed is ok provided one value is in [0..INT_MAX].
this can be allowed for compile-time constants.

The usual 'error case' is unsigned v signed when the values
are known (by the person writing the code) to be non-negative.
Doing '(x) + 0u + 0ul + 0ull' zero extends the value without
ever masking it or 'accidentally' converting a pointer.

David

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