All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] parisc: pdc_stable.c:  Cleaning up unnecessary use of memset in conjunction with strncpy
@ 2014-09-14 16:02 Rickard Strandqvist
  2014-09-22 18:24 ` Helge Deller
  0 siblings, 1 reply; 2+ messages in thread
From: Rickard Strandqvist @ 2014-09-14 16:02 UTC (permalink / raw)
  To: James E.J. Bottomley, Helge Deller
  Cc: Rickard Strandqvist, linux-parisc, linux-kernel

Using memset before strncpy just to ensure a trailing null
character is an unnecessary double writing of a string

Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
 drivers/parisc/pdc_stable.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/parisc/pdc_stable.c b/drivers/parisc/pdc_stable.c
index 0f54ab6..76860ad 100644
--- a/drivers/parisc/pdc_stable.c
+++ b/drivers/parisc/pdc_stable.c
@@ -765,8 +765,8 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 		return -EINVAL;
 
 	/* We'll use a local copy of buf */
-	memset(in, 0, count+1);
 	strncpy(in, buf, count);
+	in[count] = '\0';
 
 	/* Current flags are stored in primary boot path entry */
 	pathentry = &pdcspath_entry_primary;
-- 
1.7.10.4


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

* Re: [PATCH] parisc: pdc_stable.c:  Cleaning up unnecessary use of memset in conjunction with strncpy
  2014-09-14 16:02 [PATCH] parisc: pdc_stable.c: Cleaning up unnecessary use of memset in conjunction with strncpy Rickard Strandqvist
@ 2014-09-22 18:24 ` Helge Deller
  0 siblings, 0 replies; 2+ messages in thread
From: Helge Deller @ 2014-09-22 18:24 UTC (permalink / raw)
  To: Rickard Strandqvist, linux-parisc, James Bottomley,
	John David Anglin

* Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>:
> Using memset before strncpy just to ensure a trailing null
> character is an unnecessary double writing of a string

Thanks for the patch Rickard!

I've modified it a little (see below) and added it to my tree.

Thanks,
Helge

> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> ---
>  drivers/parisc/pdc_stable.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/parisc/pdc_stable.c b/drivers/parisc/pdc_stable.c
> index 0f54ab6..76860ad 100644
> --- a/drivers/parisc/pdc_stable.c
> +++ b/drivers/parisc/pdc_stable.c
> @@ -765,8 +765,8 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
>  		return -EINVAL;
>  
>  	/* We'll use a local copy of buf */
> -	memset(in, 0, count+1);
>  	strncpy(in, buf, count);
> +	in[count] = '\0';
>  
>  	/* Current flags are stored in primary boot path entry */
>  	pathentry = &pdcspath_entry_primary;


parisc: pdc_stable.c: Cleaning up unnecessary use of memset in conjunction with strncpy

Using memset before strncpy just to ensure a trailing null character is
an unnecessary double writing of a string

Patch modified by Helge Deller to additionally reduce stack usage.

Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Signed-off-by: Helge Deller <deller@gmx.de>

diff --git a/drivers/parisc/pdc_stable.c b/drivers/parisc/pdc_stable.c
index 0f54ab6..e4b73c2 100644
--- a/drivers/parisc/pdc_stable.c
+++ b/drivers/parisc/pdc_stable.c
@@ -755,7 +755,7 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 {
 	struct pdcspath_entry *pathentry;
 	unsigned char flags;
-	char in[count+1], *temp;
+	char in[8], *temp;
 	char c;
 
 	if (!capable(CAP_SYS_ADMIN))
@@ -765,8 +765,9 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
 		return -EINVAL;
 
 	/* We'll use a local copy of buf */
-	memset(in, 0, count+1);
+	count = min_t(size_t, count, 7);
 	strncpy(in, buf, count);
+	in[count] = '\0';
 
 	/* Current flags are stored in primary boot path entry */
 	pathentry = &pdcspath_entry_primary;

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

end of thread, other threads:[~2014-09-22 18:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-14 16:02 [PATCH] parisc: pdc_stable.c: Cleaning up unnecessary use of memset in conjunction with strncpy Rickard Strandqvist
2014-09-22 18:24 ` Helge Deller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.