2022-12-08 13:05:03

by Yang Yang

[permalink] [raw]
Subject: [PATCH linux-next] ethtool: use strscpy() to instead of strncpy()

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL terminated strings.

Signed-off-by: Xu Panda <[email protected]>
Signed-off-by: Yang Yang <[email protected]>
---
drivers/net/ethernet/hisilicon/hns/hns_ethtool.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
index 54faf0f2d1d8..b54f3706fb97 100644
--- a/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
+++ b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
@@ -644,18 +644,15 @@ static void hns_nic_get_drvinfo(struct net_device *net_dev,
{
struct hns_nic_priv *priv = netdev_priv(net_dev);

- strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
+ strscpy(drvinfo->version, HNAE_DRIVER_VERSION,
sizeof(drvinfo->version));
- drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';

- strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
- drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
+ strscpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));

- strncpy(drvinfo->bus_info, priv->dev->bus->name,
+ strscpy(drvinfo->bus_info, priv->dev->bus->name,
sizeof(drvinfo->bus_info));
- drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';

- strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
+ strscpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
drvinfo->eedump_len = 0;
}

--
2.15.2


2022-12-08 14:21:49

by Michal Kubecek

[permalink] [raw]
Subject: Re: [PATCH linux-next] ethtool: use strscpy() to instead of strncpy()

On Thu, Dec 08, 2022 at 07:47:41PM +0800, [email protected] wrote:
> From: Xu Panda <[email protected]>
>
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.
>
> Signed-off-by: Xu Panda <[email protected]>
> Signed-off-by: Yang Yang <[email protected]>

The patch looks good to me but the subject prefix should rather be
"hns:" to reflect its target code.

Michal

> ---
> drivers/net/ethernet/hisilicon/hns/hns_ethtool.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
> index 54faf0f2d1d8..b54f3706fb97 100644
> --- a/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
> +++ b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
> @@ -644,18 +644,15 @@ static void hns_nic_get_drvinfo(struct net_device *net_dev,
> {
> struct hns_nic_priv *priv = netdev_priv(net_dev);
>
> - strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
> + strscpy(drvinfo->version, HNAE_DRIVER_VERSION,
> sizeof(drvinfo->version));
> - drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
>
> - strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
> - drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
> + strscpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
>
> - strncpy(drvinfo->bus_info, priv->dev->bus->name,
> + strscpy(drvinfo->bus_info, priv->dev->bus->name,
> sizeof(drvinfo->bus_info));
> - drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
>
> - strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
> + strscpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
> drvinfo->eedump_len = 0;
> }
>
> --
> 2.15.2


Attachments:
(No filename) (1.77 kB)
signature.asc (499.00 B)
Download all attachments

2022-12-09 04:05:04

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH linux-next] ethtool: use strscpy() to instead of strncpy()

On Thu, 8 Dec 2022 19:47:41 +0800 (CST) [email protected] wrote:
> Subject: [PATCH linux-next] ethtool: use strscpy() to instead of strncpy()

Also net-next, linux-next is an integration tree, not a development
tree. It does not accept patches.