The story is in https://bugs.gentoo.org/676062 and especially https://rt.perl.org/Public/Bug/Display.html?id=133495 This test program, taken from the above perl bug, incorrectly returns 0 when built with -O2 on gcc 8.3 and 9.1, but is fine with 7.3 and probably everything before. Building with -g only makes things work: #include <stdio.h> #define I_STDLIB #ifdef I_STDLIB #include <stdlib.h> #endif #define U32 unsigned long #define BYTEORDER 0x4321 #define U8 unsigned char #include <signal.h> #ifdef SIGBUS void bletch(int s) { exit(4); } #endif int main() { #if BYTEORDER == 0x1234 || BYTEORDER == 0x4321 volatile U8 buf[8]; volatile U32 *up; int i; if (sizeof(U32) != 4) { printf("sizeof(U32) is not 4, but %d\n", sizeof(U32)); exit(1); } fflush(stdout); #ifdef SIGBUS signal(SIGBUS, bletch); #endif buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 1; buf[4] = 0; buf[5] = 0; buf[6] = 0; buf[7] = 1; for (i = 0; i < 4; i++) { up = (U32*)(buf + i); if (! ((*up == 1 << (8*i)) || /* big-endian */ (*up == 1 << (8*(3-i))) /* little-endian */ ) ) { printf("read failed (%x)\n", *up); exit(2); } } /* write test */ for (i = 0; i < 4; i++) { up = (U32*)(buf + i); *up = 0xBeef; if (*up != 0xBeef) { printf("write failed (%x)\n", *up); exit(3); } } exit(0); #else printf("1\n"); exit(1); #endif return 0; }
You are relying on undefined behaviour.
Not sure what this progam wants to test but I see it passing (exit code zero, no prints). Why is returning 0 wrong?
It returns the alignment rather than printing. 4 is the correct return code.
(U32*)(buf + i) Is undefined behavior since buf+i will mostly be unaligned. NOTE most likely is unrolling the loop and changing the code from crashing to something which will not crash. That is valid thing to do as it was undefined beforehand.