diff mbox

powerpc/cell: strncpy does not null terminate string

Message ID 4A607185.6020302@gmail.com
State Changes Requested
Headers show

Commit Message

roel kluin July 17, 2009, 12:41 p.m. UTC
With `sizeof(string) - 1` strncpy() will null terminate the string.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
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

Comments

Arnd Bergmann July 17, 2009, 3:05 p.m. UTC | #1
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 <roel.kluin@gmail.com>
> ---
> 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.

> 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 <><
roel kluin July 17, 2009, 3:19 p.m. UTC | #2
>> 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
diff mbox

Patch

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;
 }