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

From: David Laight

Date: Wed Jun 03 2026 - 05:39:43 EST


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.)

-- David

>
> IMHO, this adds non-necessary churn into the git history without
> an obvious advantage.
>
> Best Regards,
> Petr
>