https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78215
Bug ID: 78215 Summary: aggressive-loop-optimizations undefined behavior warning does not trigger Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: paf at cray dot com Target Milestone: --- Created attachment 39968 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39968&action=edit reproducer On GCC 4.8.5 from Red Hat* and GCC 7.0.0 20161104** (built with only --disable-multib option), the undefined behavior warning from aggressive-loop-optimization does not trigger with certain loops. gcc invocation: gcc -g -Wall -O2 -Werror loop_error.c The program (attached in both .c and .i versions) is a simple for loop with undefined behavior, resulting in an infinite loop (i is an int, 'value' is an unsigned long long): for (i = 0; i < 20000; i++) { /* Remove this break and the compiler warns correctly - * Note there is no break in the original code. */ /* Overflow of signed int occurs at i = 205 */ if (i == 206) break; container->value = i * 1024 * 1024 *10; printf("i: %d\n",i); } The compiler fails to warn on this construct and generates an infinite loop. The original code (which is much more complex and has dependencies) did not have a 'break', but had a number of functional and print calls. I was unable to reproduce the problem without a break statement, but I believe it can be done. The compiler compiles the program as above/as attached without any warnings/errors. If the break is removed, it warns (-Werror is set): ./loop_error.c: In function ‘main’: ./loop_error.c:33:38: error: iteration 205u invokes undefined behavior [-Werror=aggressive-loop-optimizations] container->value = i * 1024 * 1024 *10; ^ ./loop_error.c:27:2: note: containing loop for (i = 0; i < 20000; i++) { So the bug is a failure to warn on undefined behavior in some cases. *gcc -v output: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) **gcc -v output: Using built-in specs. COLLECT_GCC=./gcc COLLECT_LTO_WRAPPER=/home/build/gcc_install/usr/local/bin/../libexec/gcc/x86_64-pc-linux-gnu/7.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc/configure --disable-multilib Thread model: posix gcc version 7.0.0 20161104 (experimental) (GCC)