On 9 October 2012 08: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?

This is what the C99 standard says:

  An array of character type may be initialized by a character string
  literal, optionally enclosed in braces. Successive characters of the
  character string literal (including the terminating null character
  if there is room or if the array is of unknown size) initialize the
  elements of the array.

In your case, the array will be initialised with the numbers 1-5, the
null terminator of the string literal is ignored.  Initialisers like
this are fairly common and well-defined by the spec, so warning about
them would be wrong IMHO.

-- 
Mans Rullgard / mru

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

Reply via email to