https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128054
Bug ID: 128054
Summary: wrong code at -O2 and above: program traps on a valid
path before a call that exits
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kyuwoncho18 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
#include <stdlib.h>
volatile int sel = 1;
int x = 5, y = 42;
__attribute__((noipa)) void maybe_exit(int c) {
if (c == 42)
exit(0);
}
int main(void) {
int *p1, *p2;
if (sel) {
p1 = 0;
p2 = &y;
} else {
p1 = &x;
p2 = 0;
}
int v = *p2;
maybe_exit(v);
return *p1;
}
godbolt: https://godbolt.org/z/zMPd9Yxr7
With -O0, -O1 or -O2 -fno-isolate-erroneous-paths-dereference, maybe_exit (42)
calls exit (0) before the null *p1 is reached, so the program exits with status
0, which is correct. With -O2, -O3 or -Os it dies with SIGILL (ud2) without
calling maybe_exit.