Re: [PATCH] lib/vsprintf: replace min_t/max_t with min/max

From: Petr Mladek

Date: Wed Jun 03 2026 - 08:23:40 EST


On Wed 2026-06-03 10:35:39, David Laight wrote:
> On Thu, 21 May 2026 15:49:12 +0200
> Petr Mladek <pmladek@xxxxxxxx> wrote:
>
> > On Mon 2026-05-18 14:31:47, Thorsten Blum wrote:
> > > Use the simpler min()/max() macros since the values are all compatible.
> > >
> > > --- a/lib/vsprintf.c
> > > +++ b/lib/vsprintf.c
> > > @@ -1208,7 +1208,7 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
> > > }
> > >
> > > if (spec.field_width > 0)
> > > - len = min_t(int, spec.field_width, 64);
> > > + len = min(spec.field_width, 64);
> >
> > Honestly, I do not see any big advantage in replacing the macros.
> > In fact, the min()/max() macros are even more complex because
> > they check compatibility of the compared types.
>
> But min_t() is often worse that just letting the compiler promote
> a negative signed value to a very large unsigned one.
>
> There are basically three common misuses:
> 1) Using the type you want the result to be.
> 2) Using the smaller type, including 'long' v 'u64' on 32bit.
> 3) Thinking min_t(type, a, b) is the best way to compare items of
> type 'type'.
>
> All not helped by checkpatch suggesting that:
> min(foo, (type)bar)
> is 'an opportunity for':
> min_t(type, foo, bar)
> but the latter is:
> min((type)foo, (type)bar)
> and you never need both casts.
>
> There are also a few of the pointless/buggy:
> min_t(u8, foo, U8_MAX)
> but you'd notice that the test:
> if ((u8)foo > U8_MAX)
> doesn't do what was intended.
>
> Ideally min_t() ought to be killed, but it is a big job.
> (The sort of thing Linus could because of his 'god' bit.)

Andy, David, thanks for feedback. Fair enough.
Thorsten, thanks for the patch.

I have just pushed the patch into printk/linux.git,
branch for-7.2.

Best Regards,
Petr