https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120574
Bug ID: 120574 Summary: Inconsistent array size overflow behavior between -O0 and others optimizations: code compiles at -O1、-O2、O3, but fails at -O0 Product: gcc Version: 14.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: 2023091106 at cauc dot edu.cn Target Milestone: --- When compiling the following code using GCC with different optimization levels, -O0 results in a compilation error due to excessive local object size, while -O1 or other optimizations and above compile and run successfully. This inconsistency may hide bugs or undefined behavior depending on optimization flags. Tested with: gcc (GCC) 12/13/14 ========the gcc code======== #include <stdio.h> void a(void) { puts("A"); } void b() { char arr[(size_t)-1 / 2]; // Test for large array allocation issues arr[(size_t)-1 / 2]='a'; printf("Array size: %zu\n", sizeof(arr)); } void c() { int arr[((size_t)-1)/2 / sizeof(int)]; // Test for integer division and overflow arr[((size_t)-1)/2 / sizeof(int)]='a'; printf("Integer array size: %zu\n", sizeof(arr)); } int main(void) { b(); c(); } ========output======== $ gcc-14 -Wall -Wextra -O0 -o test test.c test.c: 在function ‘b’中: test.c:7:6: error:total size of local objects 9223372036854775808 exceeds maximum 9223372036854775296 7 | void b() { | ^ test.c: in function ‘c’中: test.c:13:6: error:total size of local objects 9223372036854775808 exceeds maximum 9223372036854775296 13 | void c() { | ^ $ gcc-14 -Wall -Wextra -O1 -o test test.c $ ./test Array size: 9223372036854775807 Integer array size: 9223372036854775804 ========the gcc versions========= $ gcc-14 -v Using built-in specs. COLLECT_GCC=gcc-14 COLLECT_LTO_WRAPPER=/opt/gcc-14/libexec/gcc/x86_64-pc-linux-gnu/14.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure --prefix=/opt/gcc-14 --enable-languages=c,c++ --disable-multilib Thread model: posix Supported LTO compression algorithms: zlib gcc version 14.1.0 (GCC)