aliasing between internal zero-length-arrays and other members

Martin Sebor msebor@gmail.com
Mon Jun 4 23:39:00 GMT 2018


GCC silently (without -Wpedantic) accepts declarations of zero
length arrays that are followed by other members in the same
struct, such as in:

   struct A { char a, b[0], c; };

Is it intended that accesses to elements of such arrays that
alias other members be well-defined?

In my tests, GCC assumes that neither read nor write accesses
to elements of internal zero-length arrays alias other members,
so assuming those aren't bugs I wonder if the documentation
should be updated to make that clear and a warning added for
such declarations (and perhaps also accesses).

For example, the test in the following function is eliminated,
implying that GCC assumes that the access to p->b does not modify
p->c, even though with i set to 0 it would:

   void f (struct A *p, int i)
   {
     int x = p->c;
     p->b[i] = 1;
     if (x != p->c)
       __builtin_abort ();
   }

Martin



More information about the Gcc mailing list