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

            Bug ID: 121952
           Summary: [9 regression] produces incorrect code with
                    optimisation on x86-64 Linux
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joey.teng.dev at gmail dot com
  Target Milestone: ---

The problematic code:

```
#include<stdio.h>
int main() {
    bool ac[19];  // must be >= 19 for bug to be seen
    long long n;
    scanf("%lld", &n);
    printf("before: %d\n", ac[n]);
    for (int i = 1; i <= n; i++) {
        ac[i] = ((n - i + 1) % 2) == 0;
    }
    const bool expected = (((n - n + 1) % 2) == 0);
    printf("Actual result: %d\nExpected: %d\n", ac[n], expected);
    printf("stdin: %lld\narray size: %ld\n", n, sizeof(ac));

    if (ac[n] != expected) {
        printf("BUG FOUND!!\n");
        return 1;
    }

    return 0;
}
```

To reproduce it, use a GCC-9 compiler (e.g. gcc-9.5) under x86-64 Linux (I was
using Ubuntu 22.04), and with compilation option `-O2`. For the standard input,
please uses `1`.

A reproduction using godbolt: https://godbolt.org/z/8MEMeozKv

The first bad commit:
https://github.com/gcc-mirror/gcc/commit/2e103ae2138ede8a4db831dea30065fe0041bc58

The first good commit since the bug was introduced:
https://github.com/gcc-mirror/gcc/commit/523fe5b61119c17d9b5adf4a6a4cffa10cfaacf7

>From the first glance, the fix of this issue seems to be unintentional. Thus I
would like to be sure if the root cause is revealed and addressed. It might be
better if we can include a test to cover this.

The script and setup I used for git bisect is hosted in GitHub gist:
https://gist.github.com/JoeyTeng/4d8c8daeef3fba5c2d076a384b9d6c34

The arguments I used to configure for a GCC build is:
```
"${SRCDIR}/configure" "--prefix=${INSTALL_DIR}" --enable-languages=c,c++
--with-multilib-list=m64 --disable-bootstrap --disable-werror
--disable-libsanitizer
```

The command I used to compile the source code (problematic code above) is:

```
"${INSTALL_DIR}/bin/g++" -O3 -o "${TEST_BIN}" "${TEST_SAMPLE}"
```

Sorry I cannot select version 9.1-9.5 for this; please help me set the
correctly impacted versions.

Reply via email to