https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79986

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
The initialization of flexible array members is still broken in various ways. 
For some examples, see pr69338, pr69696, and pr69697.

The initialization in flexary11.C is a special case of a general C++ pattern of
initializing a non-static struct with a flexible array member which, for better
or worse, is accepted by GCC is most forms.  For example, all but the last
return statement in the test case below are accepted (though they all generate
wrong code).

I think at this late stage it's safest just to avoid the ICE (see below) defer
until GCC 8 changes to the front end that could cause breakage either in GCC
itself or in other code.

$ cat t.C && gcc -O2 -S -Wall -Wextra -xc++ t.C
int f0 ()
{
  struct A { char n, a[]; };

  return ((struct A){ 1, "123" }).a[0];   // wrong code
}

int f1 ()
{
  struct B { int n; char a[]; };

  return ((struct B){ 1, "123" }).a[0];   // wrong code
}

int f2 ()
{
  struct A { char n, a[]; };

  return ((struct A){ 1, "" }).a[0];   // wrong code
}

int f3 ()
{
  struct B { int n; char a[]; };

  return ((struct B){ 1, "" }).a[0];   // rejected
}

t.C: In function ‘int f3()’:
t.C:26:21: error: invalid use of array with unspecified bounds
   return ((struct B){ 1, "" }).a[0];   // rejected
          ~~~~~~~~~~~^~~~~~~~~~
t.C:26:21: error: invalid use of array with unspecified bounds


The following change avoids the ICE by having CHKP give up on checking flexible
array members:

diff --git a/gcc/tree-chkp.c b/gcc/tree-chkp.c
index b1ff218..bbaa3a1 100644
--- a/gcc/tree-chkp.c
+++ b/gcc/tree-chkp.c
@@ -4092,6 +4092,9 @@ chkp_process_stmt (gimple_stmt_iterator *iter, tree node,
      expression to compute it.  */
   if (!addr_last)
     {
+      if (!size)
+       return;
+
       addr_last = fold_build_pointer_plus_loc (loc, addr_first, size);
       addr_last = fold_build_pointer_plus_hwi_loc (loc, addr_last, -1);
     }

Reply via email to