Skip to contentSkip to conversation

staging: wlan-ng: replace strncpy() with strscpy()

PATCH Superseded Calvince Otieno 3 participants 3 messages Oct 13, 2023 Staging

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 staging: wlan-ng: replace strncpy() with strscpy()

Checkpatch suggests the use of strscpy() instead of strncpy(). The advantages are that it always adds a NUL terminator and it prevents a read overflow if the src string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

In this code, strscpy() and strncpy() are equivalent and it does not affect runtime behavior. The string is zeroed on the line before using memset(). The resulting string was always NUL terminated and PRISM2_USB_FWFILE is string literal "prism2_ru.fw" so it's NUL terminated.

However, even though using strscpy() does not fix any bugs, it's still nicer and makes checkpatch happy.

---
Patch version v4:
Provide a valid description of the patch

Patch version v3:
Correct the patch subject headline.
staging: wlan-ng: remove strncpy() use in favor of strscpy()

Patch version v2 :
Correct implementation of the strscpy()

drivers/staging/wlan-ng/prism2fw.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/wlan-ng/prism2fw.c +1 -1
index 5d03b2b9aab4..3ccd11041646 100644
@@ -725,7 +725,7 @@ static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
 
                if (j == -1) {  /* plug the filename */
                        memset(dest, 0, s3plug[i].len);
-                       strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
+                       strscpy(dest, PRISM2_USB_FWFILE, s3plug[i].len);
                } else {        /* plug a PDR */
                        memcpy(dest, &pda->rec[j]->data, s3plug[i].len);
                }

Patch version v1:
Replacing strncpy() with strscpy()

 drivers/staging/wlan-ng/prism2fw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/wlan-ng/prism2fw.c +1 -1
index 5d03b2b9aab4..57a99dd12143 100644
@@ -725,7 +725,7 @@ static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
 
                if (j == -1) {  /* plug the filename */
                        memset(dest, 0, s3plug[i].len);
-                       strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
+                       strscpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
                } else {        /* plug a PDR */
                        memcpy(dest, &pda->rec[j]->data, s3plug[i].len);
                }
Signature · 1 line
Calvince Otieno
#1 lore raw
Quoted 12 lines · Calvince Otieno

Checkpatch suggests the use of strscpy() instead of strncpy(). The advantages are that it always adds a NUL terminator and it prevents a read overflow if the src string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

In this code, strscpy() and strncpy() are equivalent and it does not affect runtime behavior. The string is zeroed on the line before using memset(). The resulting string was always NUL terminated and PRISM2_USB_FWFILE is string literal "prism2_ru.fw" so it's NUL terminated.

Quoted 7 lines

However, even though using strscpy() does not fix any bugs, it's still nicer and makes checkpatch happy.

---
Patch version v4:
Provide a valid description of the patch

Good.

However, you've still included the v1 patch... See below. Don't do that.

regards,
dan carpenter

Quoted 23 lines

Patch version v1:
Replacing strncpy() with strscpy()

 drivers/staging/wlan-ng/prism2fw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/wlan-ng/prism2fw.c +1 -1
index 5d03b2b9aab4..57a99dd12143 100644
@@ -725,7 +725,7 @@ static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
 
                if (j == -1) {  /* plug the filename */
                        memset(dest, 0, s3plug[i].len);
-                       strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
+                       strscpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
                } else {        /* plug a PDR */
                        memcpy(dest, &pda->rec[j]->data, s3plug[i].len);
                }
Signature · 2 lines
-- 
Calvince Otieno
#2 lore raw
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Calvince Otieno

Checkpatch suggests the use of strscpy() instead of strncpy().

Since Dan suggested another change, you could also drop the extra space before strscpy in the line above.

julia

Quoted 52 lines
Quoted 11 lines

The advantages are that it always adds a NUL terminator and it prevents a read overflow if the src string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

In this code, strscpy() and strncpy() are equivalent and it does not affect runtime behavior. The string is zeroed on the line before using memset(). The resulting string was always NUL terminated and PRISM2_USB_FWFILE is string literal "prism2_ru.fw" so it's NUL terminated.

Quoted 7 lines

However, even though using strscpy() does not fix any bugs, it's still nicer and makes checkpatch happy.

---
Patch version v4:
Provide a valid description of the patch

Good.

However, you've still included the v1 patch... See below. Don't do that.

regards,
dan carpenter

Quoted 23 lines

Patch version v1:
Replacing strncpy() with strscpy()

 drivers/staging/wlan-ng/prism2fw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/wlan-ng/prism2fw.c +1 -2
index 5d03b2b9aab4..57a99dd12143 100644
@@ -725,7 +725,7 @@ static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
 
                if (j == -1) {  /* plug the filename */
                        memset(dest, 0, s3plug[i].len);
-                       strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
+                       strscpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
                } else {        /* plug a PDR */
                        memcpy(dest, &pda->rec[j]->data, s3plug[i].len);
                }
 
--

Calvince Otieno