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

            Bug ID: 116487
           Summary: Miscompile with different optimization flags
           Product: gcc
           Version: 15.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: ---

Created attachment 59004
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59004&action=edit
The preprocessed file when using -O0

When I compiled this code with -O0 and -O1 flags, it generated different
outputs:

```c
#include <stdint.h>
#include <stdio.h>
uint32_t c[];
uint32_t d = 3;
int8_t a;
void e(uint8_t b) { d = d >> 8 ^ c[(d ^ b) & 255]; }
void f(uint32_t g) {
  e(g);
  e(0);
  e(0);
  e(0);
}
void l(uint32_t g, char *, int) { f(g); }
void m(uint32_t crc, int) { printf("checksum = %X\n", crc); }
int8_t b() {
  int64_t bf;
  int32_t bg = 76;
  int8_t *bh = &a;
  *bh = bg ^= bf || 0;
}
int main() {
  int bx, i, j;
  uint32_t crc;
  uint32_t k = 1;
  i = 0;
  for (; i < 256; i++) {
    crc = i;
    for (j = 8; j; j--)
      if (crc & 1)
        crc = crc >> 1 ^ k;
      else
        crc >>= 1;
    c[i] = crc;
  }
  b();
  l(a, "", bx);
  m(d ^ 2, bx);
}
```

It can also be found in https://godbolt.org/z/sscqo3GqP

My gcc version is gcc version 15.0.0 20240826 (experimental) (GCC). 

I built this with ../configure --disable-multilib --disable-bootstrap
--enable-languages=c,c++ --prefix=/path/to/gcc.

My system type is x86_64-pc-linux-gnu.

Commands for triggering the miscompilation:
1. gcc -O0 case.c -o case-O0 -Wfatal-errors -w
2. gcc -O1 case.c -o case-O1 -Wfatal-errors -w

The outputs are:
1. When using -O0: checksum = 2
2. When using -O1: checksum = 3

Notes: 
I tried other flags, the output is checksum = 3.
  • [Bug c/116487] New: Miscompil... yunboni at smail dot nju.edu.cn via Gcc-bugs

Reply via email to