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

            Bug ID: 127964
           Summary: Wrong code at -O2 with infinite recursive jump and
                    hang
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---

#include <stdio.h>
#include <stdint.h>

__attribute__((noinline))
static uint32_t crc32_bytes(uint32_t crc, const unsigned char *p, unsigned n)
{
  for (unsigned k = 0; k < n; k++) {
    crc ^= p[k];
    unsigned j = 0;
    for (;;) {
      if (crc & 1)
        crc = (crc >> 1) ^ 0xEDB88320u;
      else
        crc = crc >> 1;
      j++;
      if (j == 8)
        break;
    }
  }
  return crc;
}

int main(void)
{
  const unsigned char msg[] = "123456789";
  uint32_t c = crc32_bytes(0xFFFFFFFFu, msg, 9);
  printf("%08x\n", c ^ 0xFFFFFFFFu);
  return 0;
}

godbolt: https://gcc.godbolt.org/z/x5WvT8eE3

With -O2, the executable hangs due to a jump to itself; it did not happen on
-O{0,1,3}.

O2 assembly:
"crc32_bytes.constprop.0":
.L2:
        jmp     .L2
"main":
        sub     rsp, 16
        lea     rdi, [rsp+6]
        call    "crc32_bytes.constprop.0"

Reply via email to