If I compile this code up to gcc 7.1 with -O2 -Wall
(or -O3
)
#include <stdio.h>
int main (void)
{
char array [4] = {0x11,0x22,0x33,0x44};
unsigned int u32 = *(unsigned int*)array; // intentionally invoking strict aliasing UB here
printf("%x\n", u32);
return 0;
}
Then I get I diagnostic about the undefined behavior:
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
This only happens when optimizations are enabled.
In 7.2 this warning disappeared and remains disabled to this day. What happened in 7.2 that made the warning go away? As usual with gcc, the change is completely undocumented - https://gcc.gnu.org/gcc-7/changes.html doesn't say a thing about changes to strict aliasing. Compiling with or without -fstrict-aliasing
doesn't seem to matter either.
Was the behavior in 7.2 changed on purpose or is this a bug? Are there any known bug reports?