https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129286
Bug ID: 129286
Summary: Wrong code generated with -O2, part of compound
condition in while () loop ignored
Product: gcc
Version: 15.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: Alexey.Shinkin at Honeywell dot com
Target Milestone: ---
Environment: Fedora 43 Linux in a VMwvare VM, Win 11 host.
# uname -a
Linux fedora 7.3.6-100.fc43.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Nov 23 18:16:55
UTC 2026 x86_64 GNU/Linux
# gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/15/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,cobol,lto
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugzilla.redhat.com/ --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-15.3.1-build/gcc-15.3.1-20260722/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
--disable-libssp
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.3.1 20260722 (Red Hat 15.3.1-1) (GCC)
Code:
#include <stdio.h>
#define MAX_ITEMS 10
typedef struct
{
struct {
unsigned char first;
unsigned short second;
} items[MAX_ITEMS];
int third;
} test_t;
int main ()
{
int i = 0;
test_t ta = {{{1,2},{3,4}}, 5};
while ( ta.items[i].second != 0xffff && i < MAX_ITEMS ) {
printf("%d : %d.%d \n", i, ta.items[i].first, ta.items[i].second);
i++;
}
}
The bug:
gcc test.c ; ./a.out - prints out 10 entries, as expected
gcc -O2 test.c; ./a.out - prints out the content of memory until hit 0xFFFF or
segfaulted.
Looking into generated asm - the second clause ( i < MAX_ITEMS ) seems to be
completely optimized out.
Swapping clauses so i checks first : ( i < MAX_ITEMS && ta.items[i].second !=
0xFFFF ) produced working binary.
Commenting out the third field in the structure - also gives working binary.
Building the test app with -m32 - the result is the same.
Observed with gcc 15.2 as well.