Re: [PATCH v2] net: mdio-gpio: replace deprecated strncpy with strscpy

From: Russell King (Oracle)
Date: Thu Dec 07 2023 - 17:57:31 EST


On Thu, Dec 07, 2023 at 09:54:31PM +0000, Justin Stitt wrote:
> We expect new_bus->id to be NUL-terminated but not NUL-padded based on
> its prior assignment through snprintf:
> | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
>
> We can also use sizeof() instead of a length macro as this more closely
> ties the maximum buffer size to the destination buffer.

Honestly, this looks machine generated and unreviewed by the submitter,
because...

> if (bus_id != -1)
> snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
> else
> - strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
> + strscpy(new_bus->id, "gpio", sizeof(new_bus->id));

If there is an argument for not using MII_BUS_ID_SIZE in one place,
then the very same argument applies to snprintf(). If one place
changes the other also needs to be changed.

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!