2023-10-30 12:01:25

by Nancy Nyambura

[permalink] [raw]
Subject: [PATCH v2 1/2] Staging: rts5208: Replace strncpy() with strscpy()

warning found by checkpatch.pl script.I replaced the usage of strncpy()
with strscpy() in the code(line 524) for the buf buffer to improve
string copying.This is appropriate since strscpy() is used when you want
to copy a NUL-terminated string and the destination buffer requires
trailing NUL-padding.

Signed-off-by: Nancy Nyambura <[email protected]>
---
Changes in v2:
- Make the commit message more clearer.

drivers/staging/rts5208/rtsx_scsi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/rts5208/rtsx_scsi.c b/drivers/staging/rts5208/rtsx_scsi.c
index 08bd768ad34d..52324b8ebbc7 100644
--- a/drivers/staging/rts5208/rtsx_scsi.c
+++ b/drivers/staging/rts5208/rtsx_scsi.c
@@ -523,7 +523,7 @@ static int inquiry(struct scsi_cmnd *srb, struct rtsx_chip *chip)

if (sendbytes > 8) {
memcpy(buf, inquiry_buf, 8);
- strncpy(buf + 8, inquiry_string, sendbytes - 8);
+ strscpy(buf + 8, inquiry_string, sendbytes - 8);
if (pro_formatter_flag) {
/* Additional Length */
buf[4] = 0x33;
--
2.40.1


2023-10-30 12:18:16

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] Staging: rts5208: Replace strncpy() with strscpy()



On Mon, 30 Oct 2023, Nancy Nyambura wrote:

> warning found by checkpatch.pl script.I replaced the usage of strncpy()
> with strscpy() in the code(line 524) for the buf buffer to improve
> string copying.This is appropriate since strscpy() is used when you want
> to copy a NUL-terminated string and the destination buffer requires
> trailing NUL-padding.

Are you sure that the original string is null terminated?

Please put a space after each . in your message, for better readability.

julia

>
> Signed-off-by: Nancy Nyambura <[email protected]>
> ---
> Changes in v2:
> - Make the commit message more clearer.
>
> drivers/staging/rts5208/rtsx_scsi.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/staging/rts5208/rtsx_scsi.c b/drivers/staging/rts5208/rtsx_scsi.c
> index 08bd768ad34d..52324b8ebbc7 100644
> --- a/drivers/staging/rts5208/rtsx_scsi.c
> +++ b/drivers/staging/rts5208/rtsx_scsi.c
> @@ -523,7 +523,7 @@ static int inquiry(struct scsi_cmnd *srb, struct rtsx_chip *chip)
>
> if (sendbytes > 8) {
> memcpy(buf, inquiry_buf, 8);
> - strncpy(buf + 8, inquiry_string, sendbytes - 8);
> + strscpy(buf + 8, inquiry_string, sendbytes - 8);
> if (pro_formatter_flag) {
> /* Additional Length */
> buf[4] = 0x33;
> --
> 2.40.1
>
>
>

2023-10-30 12:29:19

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] Staging: rts5208: Replace strncpy() with strscpy()

On Mon, Oct 30, 2023 at 02:59:34PM +0300, Nancy Nyambura wrote:
> warning found by checkpatch.pl script.I replaced the usage of strncpy()
> with strscpy() in the code(line 524) for the buf buffer to improve
> string copying.This is appropriate since strscpy() is used when you want
> to copy a NUL-terminated string and the destination buffer requires
> trailing NUL-padding.
>
> Signed-off-by: Nancy Nyambura <[email protected]>
> ---
> Changes in v2:
> - Make the commit message more clearer.

Hi Nancy,

Your commit message does not explain why you used strscpy() instead of
strscpy_pad(). I have written a blog that might be helpful.

https://staticthinking.wordpress.com/2023/10/30/strcpy-strncpy-strlcpy-and-strscpy/

regards,
dan carpenter