Skip to contentSkip to conversation

net/smc: replace strncpy with strscpy

PATCH In review Pranav Tyagi 2 participants 3 messages Jun 17, 2025 – Jun 18, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/smc: replace strncpy with strscpy

Replace the deprecated strncpy() with strscpy() as the destination buffer should be NUL-terminated and does not require any trailing NUL-padding.

---
 net/smc/smc_pnet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/smc/smc_pnet.c +1 -1
index b391c2ef463f..b70e1f3179c5 100644
@@ -370,7 +370,7 @@ static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
 		goto out_put;
 	new_pe->type = SMC_PNET_ETH;
 	memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
-	strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
+	strscpy(new_pe->eth_name, eth_name, IFNAMSIZ);
 	rc = -EEXIST;
 	new_netdev = true;
 	mutex_lock(&pnettable->lock);
Signature · 1 line
2.49.0
reviewer
#1 lore raw
Quoted 14 lines · Pranav Tyagi

Replace the deprecated strncpy() with strscpy() as the destination buffer should be NUL-terminated and does not require any trailing NUL-padding.

---
 net/smc/smc_pnet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/smc/smc_pnet.c +0 -0
index b391c2ef463f..b70e1f3179c5 100644

net/smc/smc_pnet.c static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net, · line 373

Quoted 5 lines
net/smc/smc_pnet.c +1 -1
@@ -370,7 +370,7 @@ static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
 		goto out_put;
 	new_pe->type = SMC_PNET_ETH;
 	memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
-	strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
+	strscpy(new_pe->eth_name, eth_name, IFNAMSIZ);

Hi Pranav,

I think that because strscpy always results in a NULL terminated string the length argument can be increased by one to IFNAMSIZ + 1, matching the size of the destination.

But I also think that we can handle this automatically by switching to the two-argument version of strscpy() because the destination is an array.

	strscpy(new_pe->eth_name, eth_name);
Quoted 6 lines
net/smc/smc_pnet.c +0 -0
 	rc = -EEXIST;
 	new_netdev = true;
 	mutex_lock(&pnettable->lock);
Signature · 1 line
2.49.0
Signature · 1 line
pw-bot: changes-requested
#2 lore raw
Quoted 41 lines · Simon Horman
Quoted 14 lines · Pranav Tyagi

Replace the deprecated strncpy() with strscpy() as the destination buffer should be NUL-terminated and does not require any trailing NUL-padding.

---
 net/smc/smc_pnet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/smc/smc_pnet.c +0 -0
index b391c2ef463f..b70e1f3179c5 100644

net/smc/smc_pnet.c static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net, · line 373

Quoted 5 lines
net/smc/smc_pnet.c +1 -1
@@ -370,7 +370,7 @@ static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
              goto out_put;
      new_pe->type = SMC_PNET_ETH;
      memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
-     strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
+     strscpy(new_pe->eth_name, eth_name, IFNAMSIZ);

Hi Pranav,

I think that because strscpy always results in a NULL terminated string the length argument can be increased by one to IFNAMSIZ + 1, matching the size of the destination.

But I also think that we can handle this automatically by switching to the two-argument version of strscpy() because the destination is an array.

        strscpy(new_pe->eth_name, eth_name);
Quoted 6 lines
net/smc/smc_pnet.c +0 -1
      rc = -EEXIST;
      new_netdev = true;
      mutex_lock(&pnettable->lock);
--

2.49.0

Quoted 2 lines

--
pw-bot: changes-requested

Hi,

Thanks for the feedback. Apologies for the oversight. The size parameter should match the size of the destination. Anyway, I will now use the two-argument version send a v2.

Regards
Pranav Tyagi