Re: [PATCH v4 1/3] usb: typec: ucsi: Limit read size on v1.2

From: Andy Shevchenko
Date: Fri Feb 09 2024 - 14:40:17 EST


On Fri, Feb 09, 2024 at 10:01:07AM -0800, Abhishek Pandit-Subedi wrote:
> On Fri, Feb 9, 2024 at 6:28 AM Andy Shevchenko
> <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
> > On Thu, Feb 08, 2024 at 10:02:38PM -0800, Abhishek Pandit-Subedi wrote:

..

> > > + if (ucsi->version <= UCSI_VERSION_1_2)
> > > + buf_size = min_t(size_t, 16, buf_size);
> >
> > Please, avoid using min_t(). Here the clamp() can be used.
> I think this is likely the 4th time I've been tripped up by an
> undocumented practice in this patch series. <linux/minmax.h> says
> nothing about avoiding min_t -- why prefer clamp()?

While in this case it will work correctly, the size_t is unsigned type and 16
is signed, while buf_size is unknown in this context. It means if buf_size is
signed, the min_t gives wrong result. clamp() is better choice.

See also, e.g., https://lore.kernel.org/all/20231004064220.31452-1-biju.das.jz@xxxxxxxxxxxxxx/.

> Please add the
> recommendation here
> (https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/minmax.h#n10)
> and I am more than happy to change it after.

It's not my recommendation :-)

https://lore.kernel.org/all/CAHk-=whwEAc22wm8h9FESPB5X+P4bLDgv0erBQMa1buTNQW7tA@xxxxxxxxxxxxxx/

Feel free to submit a patch.

..

> > Shouldn't magic number be defined?
> The comment right above this line documents the number.
> As this is the only use right now, I don't see a need to make it a
> macro/constant yet.

OK.

--
With Best Regards,
Andy Shevchenko