Re: [PATCH 28/44] drivers/usb/storage: use min() instead of min_t()

From: Alan Stern

Date: Wed Nov 19 2025 - 21:59:48 EST


On Wed, Nov 19, 2025 at 10:41:24PM +0000, david.laight.linux@xxxxxxxxx wrote:
> From: David Laight <david.laight.linux@xxxxxxxxx>
>
> min_t(unsigned int, a, b) casts an 'unsigned long' to 'unsigned int'.
> Use min(a, b) instead as it promotes any 'unsigned int' to 'unsigned long'
> and so cannot discard significant bits.
>
> In this case the 'unsigned long' value is small enough that the result
> is ok.
>
> Detected by an extra check added to min_t().

In fact, min_t(T, a, b) cannot go wrong as long as all the types are
unsigned and at least one of a, b has type T or smaller. Of course, in
this situation there's no reason not to simply use min(). (And if both
a and b have types larger than T, why would someone use min_t() like
this in the first place?)

Regardless, the patch is fine with me.

Acked-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>

Alan Stern

> Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
> ---
> drivers/usb/storage/protocol.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/usb/storage/protocol.c b/drivers/usb/storage/protocol.c
> index 9033e505db7f..0cff54ad90fa 100644
> --- a/drivers/usb/storage/protocol.c
> +++ b/drivers/usb/storage/protocol.c
> @@ -139,8 +139,7 @@ unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
> return cnt;
>
> while (sg_miter_next(&miter) && cnt < buflen) {
> - unsigned int len = min_t(unsigned int, miter.length,
> - buflen - cnt);
> + unsigned int len = min(miter.length, buflen - cnt);
>
> if (dir == FROM_XFER_BUF)
> memcpy(buffer + cnt, miter.addr, len);
> --
> 2.39.5
>