[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]


On 9 October 2012 09:53, Viresh Kumar <viresh.ku...@linaro.org> wrote:
> Hi Guys,
>
> I just found an issue in kernel code and thought we must have a
> compilation warning for such cases.
>
> Submitted a bug here:
>
> https://bugs.launchpad.net/gcc-linaro/+bug/1064218
>
> Problem description:
> -------------------------
>
> When we have following declaration:
>
> struct foo {
>       char array[5];
>       ....
> };
>
> And have a definition like:
>
> struct foo foo_abc = {
>     .array = "12345",
> };
>
> Problem here is: size of array is 5 bytes and so we can actually add a
> string with four characters only to it, as '\0' will take
> an additional space.
>
> But with my definition of foo_abc.. i had 5 characters + '\0'... that
> will make it 6 and that will overflow the array..
>
> What will actually happen here?
> - compiler will not add '\0' at all?
>  -or it will go outside of boundaries of array?
>
> --
> viresh
>
> _______________________________________________
> linaro-toolchain mailing list
> linaro-toolchain@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/linaro-toolchain

_______________________________________________
linaro-toolchain mailing list
linaro-toolchain@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-toolchain

Reply via email to