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
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.
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 <><
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 <><
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