Re: [PATCH] i2c: strncpy does not null terminate string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
> > ---
> > 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
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux GPIO]     [Linux SPI]     [Linux Hardward Monitoring]     [LM Sensors]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux