empty initialization of zero-length array

Paul Laufer pelaufer@csupomona.edu
Tue Dec 26 00:53:00 GMT 2000


Hi,

I'm new to these lists, but according to the archives it seems that some
changes were made to the way gcc 2.97 handles zero-length arrays some
time in October and November. The problem is that gcc abort()s when
initializing a zero-length array to an empty initializer.
gcc/extend.texi does not mention that initializing a zero-length array
with an empty initializer is forbidden, so I submit this bug report.
Even if it were so, gcc should error, not abort.

GCC version: gcc version 2.97 20001224 i686-pc-linux-gnu
configure options:  --enable-shared --with-gnu-as --with-gnu-ld --enable-threads
glibc 2.1.3 (included in case it is relevant)

Sample program that demonstrates the buggy behavior:

struct foo {
        int a;
        char array[];
};
int main()
{
        static struct foo bar = { 0, { } };
        return bar.a;
}

hal9000:~> gcc -v -save-temps test.c
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/specs
Configured with: ../configure --enable-shared --with-gnu-as --with-gnu-ld --enable-threads
gcc version 2.97 20001224 (experimental)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=97 -D__GNUC_PATCHLEVEL__=0 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ -D__tune_pentiumpro__ test.c test.i
GNU CPP version 2.97 20001224 (experimental) (cpplib) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/include
 /usr/local/i686-pc-linux-gnu/include
 /usr/include
End of search list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/cc1 -fpreprocessed test.i -quiet -dumpbase test.c -version -o test.s
GNU C version 2.97 20001224 (experimental) (i686-pc-linux-gnu) compiled by GNU C version 2.97 20001224 (experimental).
test.c: In function `main':
test.c:26: Internal compiler error in array_size_for_constructor, at varasm.c:4461
Please submit a full bug report.
 See <URL: http://www.gnu.org/software/gcc/bugs.html > for instructions.
hal9000:~> 

The compilation aborts in gcc/varasm.c here (around line 4460):

  /* ??? I'm fairly certain if there were no elements, we shouldn't have
     created the constructor in the first place.  */
  if (max_index == NULL_TREE)
    abort ();

If the "abort ();" is changed to "return 0;" then gcc compiles the file
and produces correct output, as compared with that produced when the empty
initializer is left out (which gcc gives missing initializer warning but
then effectively initializes the zero-length array with a empty
initializer).

Initialization of a zero-length array with an empty initializer is used
at least in the linux firewall code (see
linux/net/ipv4/netfilter/ip_tables.c line 1360 in linux 2.4.0-test12 for
an example). AFAIK, in the linux firewall code the zero-length array is
not used for the statically defined structure (hence the empty
initializer), but it is used for dynamically allocated structures of the
same type. The availability of the statically initialized structure
greatly simplifies some of the code. I can explain this more if required.

But the current gcc behavior is simply wrong. I would like to see gcc
support initialization of zero-length arrays to empty initalizers or see
a good explaination of why this is a bad idea :-)

Thank you all for your time and a wonderful compiler!
Paul Laufer

P.S. This causes gcc to die with segfault (same system as above):

union foo {     
        int a;  
        char array[0];
} u = {0};

hal9000:~> gcc -o test.o -c test.c   
test.c:23: Internal error: Segmentation fault.
Please submit a full bug report.
 See <URL: http://www.gnu.org/software/gcc/bugs.html > for instructions.
hal9000:~> 

Seems related to this comment in gcc/c-decl.c made by Richard Henderson:

            /* ??? Need to check somewhere that this is a structure
               and not a union, that this field is last, and that 
               this structure has at least one other named member.  */



More information about the Gcc-bugs mailing list