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

            Bug ID: 70166
           Summary: Wrong optimization of type punning in unions with 2d
                    array
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Type-punning is underspecified in the C standard and unions with arrays as
members push it to the limit but the following example seems to be broken in
gcc unintentionally. It aborts while it shouldn't.

extern void abort (void);

int main()
{
  union {
    double d[1][1];
    int a[1][1];
  } u, *volatile p = &u;

  **u.a = 1;
  **p->d = 0;

  if (**u.a != 0)
    abort();
}

Alternatively, if this is intentional, perhaps the following sentence in [1]
could be clarified further:

"Even with -fstrict-aliasing, type-punning is allowed, provided the memory is
accessed through the union type."

[1] https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Type-punning

Tested with -O2 on gcc 4.7.2, 4.9.2 and 6.0.0 20160310.

Reply via email to