Skip to contentSkip to conversation

i2c: strncpy does not null terminate string

PATCH NAK Roel Kluin 2 participants 4 messages Jul 17, 2009 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 i2c: strncpy does not null terminate string

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

---
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;
}
drivers/i2c/busses/i2c-omap.c +1 -1
index fdd8327..bede75d 100644
@@ -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;
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 27 lines · Roel Kluin

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

---
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;
}
drivers/i2c/busses/i2c-omap.c +0 -0
index fdd8327..bede75d 100644

drivers/i2c/busses/i2c-omap.c omap_i2c_probe(struct platform_device *pdev) · line 884

Quoted 7 lines
drivers/i2c/busses/i2c-omap.c +1 -1
@@ -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.

Signature · 1 line
Jean Delvare

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Oh, BTW...

Quoted 20 lines · Jean Delvare
Quoted 19 lines · Roel Kluin

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

---
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);
Quoted 2 lines
Quoted 2 lines
        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.

Quoted 18 lines

drivers/i2c/busses/i2c-omap.c omap_i2c_probe(struct platform_device *pdev) · line 884

Quoted 13 lines
drivers/i2c/busses/i2c-omap.c +1 -1
index fdd8327..bede75d 100644
@@ -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.

Signature · 1 line
Jean Delvare
author
#5 lore raw
In reply to a message not in the archive

strlcpy() will always null terminate the string.

---

drivers/i2c/busses/i2c-omap.c +1 -1
index fdd8327..f2b82ee 100644
@@ -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;