https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102405
Bug ID: 102405 Summary: Loop index limited by a function return value considered to result in too big array subscript Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: cazfi74 at gmail dot com Target Milestone: --- For a code where for-loop terminates before index variable gets value 25*8, compiler gives a warning that the variable/8 used as array subscript has value of 25 and is thus above array bounds. Warning (turned to an error by -Werror): In function 'pick_random_tech_to_lose', inlined from 'update_bulbs' at ../../../src/server/techtools.c:621:42: ../../../src/server/techtools.c:674:271: error: array subscript 25 is above array bounds of 'unsigned char[25]' [-Werror=array-bounds] 674 | if (BV_ISSET(eligible_techs, i)) { | ^ In file included from ../../../src/common/player.h:31, from ../../../src/common/game.h:36, from ../../../src/server/techtools.c:27: ../../../src/common/tech.h: In function 'update_bulbs': ../../../src/common/tech.h:144:32: note: while referencing 'vec' 144 | BV_DEFINE(bv_techs, A_LAST); | ^ cc1: all warnings being treated as errors The code compiles fine with gcc-11 (and number of earlier versions). It has failed for some weeks with the Debian's gcc-12 development snapshot, with latest update just today: $ /usr/lib/gcc-snapshot/bin/gcc --version gcc (Debian 20210918-1) 12.0.0 20210918 (experimental) [master r12-3644-g7afcb534239] Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The loop termination is determined by a function call, like this: 'for (; _index < advance_count(); _index++) {' If 'advance_count()' is replaced by '(25*8)', it compiles fine. The compiler doesn't know what values advance_count() can return (it's implemented in a different source file) - the compile fails even if it's implemented as simple 'return 25*8;' There's a lot of macro magic around in the original code (if you think that the warning is what one should get from the attached .i, then the bug is probably preprocessor side) The command line: /usr/lib/gcc-snapshot/bin/gcc -v -save-temps -DHAVE_CONFIG_H -I. -I../../../src/server -I../gen_headers -I../../../src/ai -I../../../src/ai/classic -I../../../src/common -I../../../src/common/aicore -I../../../src/common/scriptcore -I../../../src/utility -I../../../src/server/advisors -I../../../src/server/generator -I../../../src/server/scripting -DLOCALEDIR="/usr/local/share/locale" -DBINDIR="/usr/local/bin" -DFC_CONF_PATH="/usr/local/etc/freeciv" -DDEFAULT_DATA_PATH=".:data:~/.freeciv/2.6:/usr/local/share/freeciv" -DDEFAULT_SAVE_PATH=".:~/.freeciv/saves" -DDEFAULT_SCENARIO_PATH=".:data/scenarios:~/.freeciv/2.6/scenarios:~/.freeciv/scenarios:/usr/local/share/freeciv/scenarios" -Wall -Wpointer-arith -Wcast-align -Werror -Wmissing-prototypes -Wmissing-declarations -Wformat -Wformat-security -Wnested-externs -Wshadow -Wold-style-declaration -Wno-tautological-compare -Wno-nonnull-compare -g -O2 -MT techtools.lo -MD -MP -MF .deps/techtools.Tpo -c ../../../src/server/techtools.c -o techtools.o