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

            Bug ID: 117498
           Summary: Miscompile with -O3 and -O0/1/2
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yunboni at smail dot nju.edu.cn
  Target Milestone: ---

When I compiled with this code with -O0/1/2, its output is 0. But with -O3, it
triggered SIGSEGV. With -Wall -Wextra, O0/1/2 has no warnings, but with -O3,
multiple warnings raised: 

int printf(const char *, ...);
int a, d, f;
char g;
void *malloc(unsigned long);
int b() {
  int *c = malloc(sizeof(int));
  if (c == 0)
    return -1;
  return 1;
}
void e(int h, int i, char *k, char *m) {
  for (; d < i; d += 2) {
    int j = 0;
    for (; j < h; j++)
      m[j] = k[4 * j];
  }
}
void l(long h) {
  char n = 0;
  e(h, 4, &n, &g);
}
int main() {
  f = b();
  l((unsigned char)f - 4);
  printf("%X\n", a);
}

The earliest version that triggers this bug is x86_64 gcc 13.1. Details can be
found here: https://godbolt.org/z/h7Mcfo4hq

Reply via email to