linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: speakup: Replace strncpy with memcpy
@ 2018-07-01 20:57 Guenter Roeck
  2018-07-01 22:10 ` Samuel Thibault
  0 siblings, 1 reply; 2+ messages in thread
From: Guenter Roeck @ 2018-07-01 20:57 UTC (permalink / raw)
  To: Chris Brannon
  Cc: devel, Greg Kroah-Hartman, Kirk Reiser, linux-kernel,
	Samuel Thibault, speakup, William Hubbs, Guenter Roeck

gcc 8.1.0 generates the following warnings.

drivers/staging/speakup/kobjects.c: In function 'punc_store':
drivers/staging/speakup/kobjects.c:522:2: warning:
	'strncpy' output truncated before terminating nul
	copying as many bytes from a string as its length
drivers/staging/speakup/kobjects.c:504:6: note: length computed here

drivers/staging/speakup/kobjects.c: In function 'synth_store':
drivers/staging/speakup/kobjects.c:391:2: warning:
	'strncpy' output truncated before terminating nul
	copying as many bytes from a string as its length
drivers/staging/speakup/kobjects.c:388:8: note: length computed here

Using strncpy() is indeed less than perfect since the length of data to
be copied has already been determined with strlen(). Replace strncpy()
with memcpy() to address the warning and optimize the code a little.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/staging/speakup/kobjects.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/speakup/kobjects.c b/drivers/staging/speakup/kobjects.c
index f1f90222186b..08f11cc17371 100644
--- a/drivers/staging/speakup/kobjects.c
+++ b/drivers/staging/speakup/kobjects.c
@@ -388,7 +388,7 @@ static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
 	len = strlen(buf);
 	if (len < 2 || len > 9)
 		return -EINVAL;
-	strncpy(new_synth_name, buf, len);
+	memcpy(new_synth_name, buf, len);
 	if (new_synth_name[len - 1] == '\n')
 		len--;
 	new_synth_name[len] = '\0';
@@ -519,7 +519,7 @@ static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
 		return -EINVAL;
 	}
 
-	strncpy(punc_buf, buf, x);
+	memcpy(punc_buf, buf, x);
 
 	while (x && punc_buf[x - 1] == '\n')
 		x--;
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] staging: speakup: Replace strncpy with memcpy
  2018-07-01 20:57 [PATCH] staging: speakup: Replace strncpy with memcpy Guenter Roeck
@ 2018-07-01 22:10 ` Samuel Thibault
  0 siblings, 0 replies; 2+ messages in thread
From: Samuel Thibault @ 2018-07-01 22:10 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Chris Brannon, devel, Greg Kroah-Hartman, Kirk Reiser,
	linux-kernel, speakup, William Hubbs

Guenter Roeck, le dim. 01 juil. 2018 13:57:24 -0700, a ecrit:
> gcc 8.1.0 generates the following warnings.
> 
> drivers/staging/speakup/kobjects.c: In function 'punc_store':
> drivers/staging/speakup/kobjects.c:522:2: warning:
> 	'strncpy' output truncated before terminating nul
> 	copying as many bytes from a string as its length
> drivers/staging/speakup/kobjects.c:504:6: note: length computed here
> 
> drivers/staging/speakup/kobjects.c: In function 'synth_store':
> drivers/staging/speakup/kobjects.c:391:2: warning:
> 	'strncpy' output truncated before terminating nul
> 	copying as many bytes from a string as its length
> drivers/staging/speakup/kobjects.c:388:8: note: length computed here
> 
> Using strncpy() is indeed less than perfect since the length of data to
> be copied has already been determined with strlen(). Replace strncpy()
> with memcpy() to address the warning and optimize the code a little.
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>

Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

> ---
>  drivers/staging/speakup/kobjects.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/speakup/kobjects.c b/drivers/staging/speakup/kobjects.c
> index f1f90222186b..08f11cc17371 100644
> --- a/drivers/staging/speakup/kobjects.c
> +++ b/drivers/staging/speakup/kobjects.c
> @@ -388,7 +388,7 @@ static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
>  	len = strlen(buf);
>  	if (len < 2 || len > 9)
>  		return -EINVAL;
> -	strncpy(new_synth_name, buf, len);
> +	memcpy(new_synth_name, buf, len);
>  	if (new_synth_name[len - 1] == '\n')
>  		len--;
>  	new_synth_name[len] = '\0';
> @@ -519,7 +519,7 @@ static ssize_t punc_store(struct kobject *kobj, struct kobj_attribute *attr,
>  		return -EINVAL;
>  	}
>  
> -	strncpy(punc_buf, buf, x);
> +	memcpy(punc_buf, buf, x);
>  
>  	while (x && punc_buf[x - 1] == '\n')
>  		x--;
> -- 
> 2.7.4
> 

-- 
Samuel
Tu as lu les docs. Tu es devenu un informaticien. Que tu le veuilles
ou non. Lire la doc, c'est le Premier et Unique Commandement de
l'informaticien.
-+- TP in: Guide du Linuxien pervers - "L'évangile selon St Thomas"

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-07-01 22:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-01 20:57 [PATCH] staging: speakup: Replace strncpy with memcpy Guenter Roeck
2018-07-01 22:10 ` Samuel Thibault

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).