$ cat test.cpp void f(); int c[3]; int result;
struct Vector { static int get(int i) { if (i >= 3) f(); return c[i]; } }; void g() { for (int i = 0; i < 3; ++i) { const int index = i % 3; result = Vector::get(index) + Vector::get(index); } } $ g++ -Wall -c test.cpp [no warnings] $ g++ -Wall -c -O2 test.cpp test.cpp: In function void g(): test.cpp:10: error: array subscript is above array bounds $ g++ -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.2-9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.3 20100108 (prerelease) (Debian 4.4.2-9) The bogus warning disappears if - (index) is replaced by (i) - Vector::get(index) is assigned to result (no addition) - the if statement is removed (call of f() in all cases) - get() is a global function The bogus warning does not appear in gcc 4.2.4 (Debian 4.2.4-6). It appears also in 4.3.2 (Debian 4.3.2-1.1) and gcc 4.5.0 (GCC). -- Summary: bogus warning: array subscript is above array bounds Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: joachim dot reichel at gmx dot de GCC build triplet: x86_64-linux-gnu GCC host triplet: x86_64-linux-gnu GCC target triplet: x86_64-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949