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

            Bug ID: 111319
           Summary: Wrong code at -O2 on x86_64-linux-gnu
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
  Target Milestone: ---

The executable produced by gcc -O2 would crash. Not sure if there is UB in the
code or not as `h` has not been dereferenced after `h--`.

Compiler explorer: https://godbolt.org/z/avssssPze

$ cat a.c
int printf(const char *, ...);
int a, b = 5, c, d;
int e[2] = {0, 0};
int i() {
  int f = 0;
  int g[2] = {1, f};
  int *h = g;
  h--;
  if (!b)
    while (h >= &c == 1)
      h--;
  h++;
  d = *h;
  return d;
}
int main() {
  if (e[i()])
    e[0] = 0;
  printf("%d\n", a);
}
$
$ gcc -O2 a.c && ./a.out
Segmentation fault
$

Reply via email to