From: David Laight <david.laight.linux@gmail.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org,
usb-storage@lists.one-eyed-alien.net,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH 28/44] drivers/usb/storage: use min() instead of min_t()
Date: Thu, 20 Nov 2025 09:18:02 +0000 [thread overview]
Message-ID: <20251120091802.0b238c96@pumpkin> (raw)
In-Reply-To: <4208129f-a768-44e2-843f-309c50ea362f@rowland.harvard.edu>
On Wed, 19 Nov 2025 21:59:42 -0500
Alan Stern <stern@rowland.harvard.edu> wrote:
> On Wed, Nov 19, 2025 at 10:41:24PM +0000, david.laight.linux@gmail.com wrote:
> > From: David Laight <david.laight.linux@gmail.com>
> >
> > 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.
That is backwards, both a and b have to have types at least as large
as T (or rather values that will fit in T).
- which is exactly what people keep getting wrong.
Consider:
u32 a = 4;
u64 b = 0x100000001ull;
then:
min_t(u32, a, b)
has value 1 not 4.
David
> 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@rowland.harvard.edu>
>
> Alan Stern
>
> > Signed-off-by: David Laight <david.laight.linux@gmail.com>
> > ---
> > 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
> >
next prev parent reply other threads:[~2025-11-20 9:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-19 22:40 [PATCH 00/44] Change a lot of min_t() that might mask high bits david.laight.linux
2025-11-19 22:41 ` [PATCH 28/44] drivers/usb/storage: use min() instead of min_t() david.laight.linux
2025-11-20 2:59 ` Alan Stern
2025-11-20 9:18 ` David Laight [this message]
2025-11-20 14:39 ` [usb-storage] " Alan Stern
2025-11-20 1:47 ` [PATCH 00/44] Change a lot of min_t() that might mask high bits Jakub Kicinski
2025-11-20 9:38 ` Lorenzo Stoakes
2025-11-20 14:52 ` (subset) " Jens Axboe
2025-11-24 9:49 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251120091802.0b238c96@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=usb-storage@lists.one-eyed-alien.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox