Skip to contentSkip to conversation

net: qcom/emac: Replace strlcpy with strscpy

PATCH Applied Jason Wang 2 participants 2 messages Sep 6, 2021

Subsystems (3)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 net: qcom/emac: Replace strlcpy with strscpy

The strlcpy should not be used because it doesn't limit the source length. As linus says, it's a completely useless function if you can't implicitly trust the source string - but that is almost always why people think they should use it! All in all the BSD function will lead some potential bugs.

But the strscpy doesn't require reading memory from the src string beyond the specified "count" bytes, and since the return value is easier to error-check than strlcpy()'s. In addition, the implementation is robust to the string changing out from underneath it, unlike the current strlcpy() implementation.

Thus, We prefer using strscpy instead of strlcpy.

---
 drivers/net/ethernet/qualcomm/emac/emac-ethtool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/qualcomm/emac/emac-ethtool.c +1 -1
index 79e50079ed03..f72e13b83869 100644
@@ -100,7 +100,7 @@ static void emac_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 
 	case ETH_SS_STATS:
 		for (i = 0; i < EMAC_STATS_LEN; i++) {
-			strlcpy(data, emac_ethtool_stat_strings[i],
+			strscpy(data, emac_ethtool_stat_strings[i],
 				ETH_GSTRING_LEN);
 			data += ETH_GSTRING_LEN;
 		}
Signature · 1 line
2.33.0
#1 lore raw
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 6 lines · On Mon, 6 Sep 2021 21:56:53 +0800 you wrote:

The strlcpy should not be used because it doesn't limit the source length. As linus says, it's a completely useless function if you can't implicitly trust the source string - but that is almost always why people think they should use it! All in all the BSD function will lead some potential bugs.

Quoted 7 lines

But the strscpy doesn't require reading memory from the src string beyond the specified "count" bytes, and since the return value is easier to error-check than strlcpy()'s. In addition, the implementation is robust to the string changing out from underneath it, unlike the current strlcpy() implementation.

[...]

Here is the summary with links:
- net: qcom/emac: Replace strlcpy with strscpy

    https://git.kernel.org/netdev/net/c/0a83299935f0

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html