Skip to contentSkip to conversation

net/rds: use strscpy() to instead of strncpy()

PATCH In review [email protected] 2 participants 4 messages Jan 11, 2023 – Jan 13, 2023

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

PATCH v2 1/1 net/rds: 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.

---
change for v2
- Use the returns of strscpy to make the copy and the preceding
BUG_ON() together.Thanks to Jakub Kicinski.

---
 net/rds/stats.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
net/rds/stats.c +1 -3
index 9e87da43c004..7018c67418f5 100644
@@ -88,9 +88,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 	size_t i;
 
 	for (i = 0; i < nr; i++) {
-		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		BUG_ON(strscpy(ctr.name, names[i], sizeof(ctr.name)) < 0);
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
Signature · 1 line
2.15.2
maintainer
#1 lore raw
Quoted 4 lines · On Wed, 11 Jan 2023 14:25:48 +0800 (CST) [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.

What are the differences in behavior between strncpy() and strscpy()?

Quoted 15 lines
net/rds/stats.c +1 -3
index 9e87da43c004..7018c67418f5 100644
@@ -88,9 +88,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter,
 	size_t i;
 
 	for (i = 0; i < nr; i++) {
-		BUG_ON(strlen(names[i]) >= sizeof(ctr.name));
-		strncpy(ctr.name, names[i], sizeof(ctr.name) - 1);
-		ctr.name[sizeof(ctr.name) - 1] = '\0';
+		BUG_ON(strscpy(ctr.name, names[i], sizeof(ctr.name)) < 0);
 		ctr.value = values[i];
 
 		rds_info_copy(iter, &ctr, sizeof(ctr));
#2 lore raw
Quoted 1 line

What are the differences in behavior between strncpy() and strscpy()?

Strscpy() makes the dest string NUL-terminated, and returns more useful value. While strncpy() can initialize the dest string.

Here we use strscpy() to make dest string NUL-terminated, and use return value to check src string size and dest string size. This make the code simpler.

maintainer
#3 lore raw
Quoted 8 lines · On Fri, 13 Jan 2023 15:13:12 +0800 (CST) [email protected] wrote:
Quoted 1 line

What are the differences in behavior between strncpy() and strscpy()?

Strscpy() makes the dest string NUL-terminated, and returns more useful value. While strncpy() can initialize the dest string.

Here we use strscpy() to make dest string NUL-terminated, and use return value to check src string size and dest string size. This make the code simpler.

I'm not sure whether in this particular case the output needs to be padded or not. And I'm not sure you understand what the implications are.

The code is fine as is, and I don't trust that you know what you're doing. So please don't send any more strncpy() -> strscpy() conversions for networking.

If you want to do something useful please start with adding a check to checkpatch to warn people against using strncpy() and suggest using strscpy() instead.