On 9 October 2012 13:47, Christophe Lyon <[email protected]> wrote:
> [copy of my answer in your bug report]
>
> Hello,
>
> If you look at the assembly code generated by the compiler, you will see:
> foo_abc:
> .ascii "12345"
>
> From the gas documentation:
> `.ascii' expects zero or more string literals (*note Strings::)
> separated by commas. It assembles each string (with no automatic
> trailing zero byte) into consecutive addresses.
>
> So, '\0' is NOT added at the end of the initializer.
>
> If you change your initialization to "123456", GCC prints a warning:
> warning: initializer-string for array of chars is too long [enabled by
> default]
> warning: (near initialization for 'foo_abc.array') [enabled by default]
Something new, At least for me :)
This means code like below can break anytime :(
char foo1[4] = "test";
char foo2[4];
void mystrcpy(char *src, char *dst)
{
/* Well known strcpy implementation */
while (*dst++ = *src++);
}
void main(void)
{
mystrcpy(foo1, foo2);
printf("src: %s and dst: %s\n", foo1, foo2);
}
As, nobody is guarantees that location next to foo1 is going to be '\0'?
--
viresh
_______________________________________________
linaro-toolchain mailing list
[email protected]
http://lists.linaro.org/mailman/listinfo/linaro-toolchain