[Bug tree-optimization/126820] New: Different behavior at -O0 and -O2/-O3 with __builtin_calloc

stuzyf at 163 dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126820

            Bug ID: 126820
           Summary: Different behavior at -O0 and -O2/-O3 with
                    __builtin_calloc
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stuzyf at 163 dot com
  Target Milestone: ---
            Target: x86-64

The following testcase behaves differently depending on the optimization level:
Reproducer: https://godbolt.org/z/c6Yv5bPMW

```
#include <stdlib.h>

int fun(int x) { volatile int y = x; return y; }

int main ()
{
  if (__builtin_calloc ((~(__SIZE_TYPE__) 0) / 16, fun(64)) )
    __builtin_abort ();
}
```

On x86-64 with GCC trunk, compiling with -O2 or -O3 produces a program that
terminates with SIGSEGV:

```
ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 139
Program terminated with signal: SIGSEGV
```

Without optimization, the program returns normally:

```
ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
```

This issue is reproducible with GCC 15.1 and later, but not with the older GCC
versions I tested.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.