Skip to contentSkip to conversation

parisc: use strscpy() to instead of strncpy()

PATCH In review [email protected] 3 participants 3 messages Dec 26, 2022 – Dec 27, 2022 PA-RISC architecture

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 parisc: 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
- sizeof(in) is better and simplified, thanks for Helge Deller.

---
 drivers/parisc/pdc_stable.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +3 -6
index d6af5726ddf3..d3075445260b 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, sizeof(in));
 	
 	/* Let's clean up the target. 0xff is a blank pattern */
 	memset(&hwpath, 0xff, sizeof(hwpath));
@@ -388,8 +387,7 @@ pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, sizeof(in));
 	
 	/* Let's clean up the target. 0 is a blank pattern */
 	memset(&layers, 0, sizeof(layers));
@@ -756,8 +754,7 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, sizeof(in));
 
 	/* Current flags are stored in primary boot path entry */
 	pathentry = &pdcspath_entry_primary;
Signature · 1 line
2.15.2
#1 lore raw

Am Montag, 26. Dezember 2022, 11:40:04 CET schrieb [email protected]:

Quoted 21 lines

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
- sizeof(in) is better and simplified, thanks for Helge Deller.

---
 drivers/parisc/pdc_stable.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..d3075445260b 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry *entry,

const char *buf, size_t coun

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 5 lines
drivers/parisc/pdc_stable.c +1 -2
 	/* We'll use a local copy of buf */
 	count = min_t(size_t, count, sizeof(in)-1);
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, sizeof(in));

What is "count" now needed for? Looks like a write only variable at least in these hunks.

Eike

maintainer
#2 lore raw
Quoted 22 lines · Rolf Eike Beer

Am Montag, 26. Dezember 2022, 11:40:04 CET schrieb [email protected]:

Quoted 21 lines

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
- sizeof(in) is better and simplified, thanks for Helge Deller.

---
  drivers/parisc/pdc_stable.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)
drivers/parisc/pdc_stable.c +0 -0
index d6af5726ddf3..d3075445260b 100644
@@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry *entry,

const char *buf, size_t coun

Quoted 8 lines

drivers/parisc/pdc_stable.c pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t coun · line 277

Quoted 5 lines

/* We'll use a local copy of buf */ count = min_t(size_t, count, sizeof(in)-1);

drivers/parisc/pdc_stable.c +1 -2
-	strncpy(in, buf, count);
-	in[count] = '\0';
+	strscpy(in, buf, sizeof(in));

What is "count" now needed for? Looks like a write only variable at least in these hunks.

isn't count the return value?

Helge