All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c: strncpy does not null terminate string
@ 2009-07-17 13:03 Roel Kluin
       [not found] ` <4A60769C.9010707-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Roel Kluin @ 2009-07-17 13:03 UTC (permalink / raw)
  To: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA, Andrew Morton

With `sizeof(string) - 1` strncpy() will null terminate the string.

Signed-off-by: Roel Kluin <roel.kluin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
To test this:

#include <stdio.h>
#include <string.h>

char a[10];
char b[10];

int main()
{
        const char* str = "0123456789012";
        strncpy(a, str, sizeof(a));
        strncpy(b, str, sizeof(b) - 1);
        printf("String a was %s, b was %s\n", a, b);

        return 0;
}

diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
index fdd8327..bede75d 100644
--- a/drivers/i2c/busses/i2c-omap.c
+++ b/drivers/i2c/busses/i2c-omap.c
@@ -879,7 +879,7 @@ omap_i2c_probe(struct platform_device *pdev)
 	i2c_set_adapdata(adap, dev);
 	adap->owner = THIS_MODULE;
 	adap->class = I2C_CLASS_HWMON;
-	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
+	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name) - 1);
 	adap->algo = &omap_i2c_algo;
 	adap->dev.parent = &pdev->dev;
 

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

* Re: [PATCH] i2c: strncpy does not null terminate string
       [not found] ` <4A60769C.9010707-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2009-07-17 13:06   ` Jean Delvare
       [not found]     ` <20090717150624.4a0636f7-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
  2009-07-17 13:24   ` Roel Kluin
  1 sibling, 1 reply; 4+ messages in thread
From: Jean Delvare @ 2009-07-17 13:06 UTC (permalink / raw)
  To: Roel Kluin
  Cc: ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA, Andrew Morton

On Fri, 17 Jul 2009 15:03:24 +0200, Roel Kluin wrote:
> With `sizeof(string) - 1` strncpy() will null terminate the string.
> 
> Signed-off-by: Roel Kluin <roel.kluin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> ---
> To test this:
> 
> #include <stdio.h>
> #include <string.h>
> 
> char a[10];
> char b[10];
> 
> int main()
> {
>         const char* str = "0123456789012";
>         strncpy(a, str, sizeof(a));
>         strncpy(b, str, sizeof(b) - 1);
>         printf("String a was %s, b was %s\n", a, b);
> 
>         return 0;
> }
> 
> diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
> index fdd8327..bede75d 100644
> --- a/drivers/i2c/busses/i2c-omap.c
> +++ b/drivers/i2c/busses/i2c-omap.c
> @@ -879,7 +879,7 @@ omap_i2c_probe(struct platform_device *pdev)
>  	i2c_set_adapdata(adap, dev);
>  	adap->owner = THIS_MODULE;
>  	adap->class = I2C_CLASS_HWMON;
> -	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
> +	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name) - 1);
>  	adap->algo = &omap_i2c_algo;
>  	adap->dev.parent = &pdev->dev;

Nack, please just use strlcpy() instead.

IMHO strncpy() should be banned from the kernel.

-- 
Jean Delvare

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

* Re: [PATCH] i2c: strncpy does not null terminate string
       [not found]     ` <20090717150624.4a0636f7-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
@ 2009-07-17 13:13       ` Jean Delvare
  0 siblings, 0 replies; 4+ messages in thread
From: Jean Delvare @ 2009-07-17 13:13 UTC (permalink / raw)
  To: Roel Kluin
  Cc: ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA, Andrew Morton

Oh, BTW...

On Fri, 17 Jul 2009 15:06:24 +0200, Jean Delvare wrote:
> On Fri, 17 Jul 2009 15:03:24 +0200, Roel Kluin wrote:
> > With `sizeof(string) - 1` strncpy() will null terminate the string.
> > 
> > Signed-off-by: Roel Kluin <roel.kluin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > ---
> > To test this:
> > 
> > #include <stdio.h>
> > #include <string.h>
> > 
> > char a[10];
> > char b[10];
> > 
> > int main()
> > {
> >         const char* str = "0123456789012";
> >         strncpy(a, str, sizeof(a));
> >         strncpy(b, str, sizeof(b) - 1);
> >         printf("String a was %s, b was %s\n", a, b);
> > 
> >         return 0;
> > }

You can't compare user-space code and kernel code, as libc
implementations can differ. For example, snprintf() in user-space may
not NULL-terminate the string while the kernel implementation always does.

> > 
> > diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
> > index fdd8327..bede75d 100644
> > --- a/drivers/i2c/busses/i2c-omap.c
> > +++ b/drivers/i2c/busses/i2c-omap.c
> > @@ -879,7 +879,7 @@ omap_i2c_probe(struct platform_device *pdev)
> >  	i2c_set_adapdata(adap, dev);
> >  	adap->owner = THIS_MODULE;
> >  	adap->class = I2C_CLASS_HWMON;
> > -	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
> > +	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name) - 1);
> >  	adap->algo = &omap_i2c_algo;
> >  	adap->dev.parent = &pdev->dev;
> 
> Nack, please just use strlcpy() instead.
> 
> IMHO strncpy() should be banned from the kernel.
> 


-- 
Jean Delvare

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

* Re: [PATCH] i2c: strncpy does not null terminate string
       [not found] ` <4A60769C.9010707-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  2009-07-17 13:06   ` Jean Delvare
@ 2009-07-17 13:24   ` Roel Kluin
  1 sibling, 0 replies; 4+ messages in thread
From: Roel Kluin @ 2009-07-17 13:24 UTC (permalink / raw)
  Cc: khali-PUYAD+kWke1g9hUCZPvPmw, ben-linux-elnMNo+KYs3YtjvyW6yDsg,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA, Andrew Morton

strlcpy() will always null terminate the string.

Signed-off-by: Roel Kluin <roel.kluin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
index fdd8327..f2b82ee 100644
--- a/drivers/i2c/busses/i2c-omap.c
+++ b/drivers/i2c/busses/i2c-omap.c
@@ -879,7 +879,7 @@ omap_i2c_probe(struct platform_device *pdev)
 	i2c_set_adapdata(adap, dev);
 	adap->owner = THIS_MODULE;
 	adap->class = I2C_CLASS_HWMON;
-	strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
+	strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
 	adap->algo = &omap_i2c_algo;
 	adap->dev.parent = &pdev->dev;
 

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

end of thread, other threads:[~2009-07-17 13:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-17 13:03 [PATCH] i2c: strncpy does not null terminate string Roel Kluin
     [not found] ` <4A60769C.9010707-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2009-07-17 13:06   ` Jean Delvare
     [not found]     ` <20090717150624.4a0636f7-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-07-17 13:13       ` Jean Delvare
2009-07-17 13:24   ` Roel Kluin

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.