[PATCH] powerpc/cell: strncpy does not null terminate string

Subsystems: cell broadband engine architecture, linux for powerpc (32-bit and 64-bit), the rest

STALE6261d

8 messages, 3 authors, 2009-07-22 · open the first message on its own page

[PATCH] powerpc/cell: strncpy does not null terminate string

From: Roel Kluin <hidden>
Date: 2009-07-17 12:39:52

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

Signed-off-by: Roel Kluin <redacted>
---
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;
}

Output:
String a was 0123456789012345678, b was 012345678
diff --git a/arch/powerpc/platforms/cell/celleb_setup.c b/arch/powerpc/platforms/cell/celleb_setup.c
index 07c234f..cfdbadb 100644
--- a/arch/powerpc/platforms/cell/celleb_setup.c
+++ b/arch/powerpc/platforms/cell/celleb_setup.c
@@ -80,7 +80,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type) - 1);
 	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
 	return 0;
 }

Re: [PATCH] powerpc/cell: strncpy does not null terminate string

From: Roel Kluin <hidden>
Date: 2009-07-17 14:33:52

strlcpy() will always null terminate the string.

Signed-off-by: Roel Kluin <redacted>
---
Please use this one instead
diff --git a/arch/powerpc/platforms/cell/celleb_setup.c b/arch/powerpc/platforms/cell/celleb_setup.c
index 07c234f..1896cd8 100644
--- a/arch/powerpc/platforms/cell/celleb_setup.c
+++ b/arch/powerpc/platforms/cell/celleb_setup.c
@@ -80,7 +80,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
 	return 0;
 }

Re: [PATCH] powerpc/cell: strncpy does not null terminate string

From: Arnd Bergmann <arnd@arndb.de>
Date: 2009-07-17 15:05:27

On Friday 17 July 2009, Roel Kluin wrote:
With `sizeof(string) - 1` strncpy() will null terminate the string.
No, it won't. See the 'Warning' part of the strncpy man page.
Signed-off-by: Roel Kluin <redacted>
---
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;
}

Output:
String a was 0123456789012345678, b was 012345678
This is an invalid test case, it relies on b being zero-filled by the
compiler, which is not true for programs in general.
quoted hunk
diff --git a/arch/powerpc/platforms/cell/celleb_setup.c b/arch/powerpc/platforms/cell/celleb_setup.c
index 07c234f..cfdbadb 100644
--- a/arch/powerpc/platforms/cell/celleb_setup.c
+++ b/arch/powerpc/platforms/cell/celleb_setup.c
@@ -80,7 +80,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type) - 1);
 	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
 	return 0;
 }
See the line after the strncpy. This is still required for proper zero-termination.

Your patch tries to address a problem that doesn't exist, and does not have any
effect at all after celleb_machine_type_hack has completed.

	Arnd <><

Re: [PATCH] powerpc/cell: strncpy does not null terminate string

From: roel kluin <hidden>
Date: 2009-07-17 15:19:18

quoted
With `sizeof(string) - 1` strncpy() will null terminate the string.
No, it won't.
See the line after the strncpy. This is still required for proper zero-termination.
You're right, sorry for the noise.

Roel

Re: [PATCH] powerpc/cell: strncpy does not null terminate string

From: Arnd Bergmann <arnd@arndb.de>
Date: 2009-07-17 15:56:50

On Friday 17 July 2009, Roel Kluin wrote:
 static int __init celleb_machine_type_hack(char *ptr)
 {
-       strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+       strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
        celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
        return 0;
This still is pointless as long as you keep the explicit null-termination
in the next line, the patch still doesn't change anything significant.

The file is maintained by Ishizaki Kou, if he would prefer to take a
patch replacing the two lines with one, that's fine with me, otherwise
I just wouldn't bother. You still only gain a few bytes of inittext, but
that is discarded at boot time.

	Arnd <><

Re: [PATCH] powerpc/cell: strncpy does not null terminate string

From: Ken Kawakami <hidden>
Date: 2009-07-21 09:31:25

Arnd-san, Roel-san,

Thanks for pointing us to the redundant cord portion.
On Friday 17 July 2009, Roel Kluin wrote:
quoted
 static int __init celleb_machine_type_hack(char *ptr)
 {
-       strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+       strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
        celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
        return 0;
This still is pointless as long as you keep the explicit null-termination
in the next line, the patch still doesn't change anything significant.

The file is maintained by Ishizaki Kou, if he would prefer to take a
patch replacing the two lines with one, that's fine with me, otherwise
I just wouldn't bother. You still only gain a few bytes of inittext, but
that is discarded at boot time.
We prefer to take the patch which is replacing the two lines with one.
-       strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+       strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
-       celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;

Thanks,
Ken Kawakami

[PATCH] powerpc/cell: replace strncpy by strlcpy

From: Roel Kluin <hidden>
Date: 2009-07-21 10:15:12

Replace strncpy() and explicit null-termination by strlcpy()

Signed-off-by: Roel Kluin <redacted>
---
Arnd-san, Ken-san,

Thanks for reviewing,
We prefer to take the patch which is replacing the two lines with one.
Doozo.
diff --git a/arch/powerpc/platforms/cell/celleb_setup.c b/arch/powerpc/platforms/cell/celleb_setup.c
index 07c234f..e538455 100644
--- a/arch/powerpc/platforms/cell/celleb_setup.c
+++ b/arch/powerpc/platforms/cell/celleb_setup.c
@@ -80,8 +80,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
-	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	return 0;
 }
 

Re: [PATCH] powerpc/cell: replace strncpy by strlcpy

From: Ken Kawakami <hidden>
Date: 2009-07-22 04:22:11

Arnd-san, Roel-san,

It works fine. Thanks.

---
Regards,
Ken Kawakami
quoted hunk
Replace strncpy() and explicit null-termination by strlcpy()

Signed-off-by: Roel Kluin <redacted>
---
Arnd-san, Ken-san,

Thanks for reviewing,
quoted
We prefer to take the patch which is replacing the two lines with one.
Doozo.
diff --git a/arch/powerpc/platforms/cell/celleb_setup.c b/arch/powerpc/platforms/cell/celleb_setup.c
index 07c234f..e538455 100644
--- a/arch/powerpc/platforms/cell/celleb_setup.c
+++ b/arch/powerpc/platforms/cell/celleb_setup.c
@@ -80,8 +80,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
-	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	return 0;
 }
 
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help