[Bug tree-optimization/82224] Strict-aliasing not noticing valid aliasing of two unions with active members

ch3root at openwall dot com gcc-bugzilla@gcc.gnu.org
Mon Oct 23 07:21:00 GMT 2017


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

Alexander Cherepanov <ch3root at openwall dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ch3root at openwall dot com

--- Comment #6 from Alexander Cherepanov <ch3root at openwall dot com> ---
Here are simplified testcases. With a union (C and C++):

----------------------------------------------------------------------
#include <stdio.h>

union u {
  long x;
  long long y;
};

static long test(long *px, long long *py, union u *pu)
{
  pu->x = 0;            // make .x active member (for C++)
  *px = 0;              // access .x via pointer

  pu->y = pu->x;        // switch active member to .y (for C++)
  *py = 1;              // access .y via pointer

  pu->x = pu->y;        // switch active member back to .x
  return *px;           // access .x via pointer
}

int main(void)
{
  union u u;

  printf("%ld\n", test(&u.x, &u.y, &u));
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
1
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
0
----------------------------------------------------------------------

And with allocated memory (C; add placement new's for C++):

----------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static long test(long *px, long long *py, void *pu)
{
  *px = 0;
  *py = 1;

  // change effective type from long long to long
  long tmp;
  memcpy(&tmp, pu, sizeof(tmp));
  memcpy(pu, &tmp, sizeof(tmp));

  return *px;
}

int main(void)
{
  void *p = malloc(10);

  printf("%ld\n", test(p, p, p));
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
1
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
0
----------------------------------------------------------------------

gcc version: gcc (GCC) 8.0.0 20171023 (experimental)


More information about the Gcc-bugs mailing list