From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C5D753858D28; Wed, 15 Dec 2021 03:11:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5D753858D28 From: "f.heckenbach@fh-soft.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior Date: Wed, 15 Dec 2021 03:11:32 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: f.heckenbach@fh-soft.de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: short_desc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Dec 2021 03:11:32 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103724 Frank Heckenbach changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|warning |invalid warning: iteration | |7 invokes undefined | |behavior --- Comment #1 from Frank Heckenbach --- % cat test.c=20 int d =3D 0, b =3D 8, a[8][8]; int main () { for (int c =3D 0; c < 11; c++) { d =3D a[c >=3D b ? c - b : c][c + 1 >=3D b ? c + 1 - b : c + 1]; if (b && c) d =3D c; } } % gcc -O3 test.c test.c: In function 'main': test.c:7:32: warning: iteration 7 invokes undefined behavior [-Waggressive-loop-optimizations] 7 | d =3D a[c >=3D b ? c - b : c][c + 1 >=3D b ? c + 1 - b : c + = 1]; | ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.c:5:3: note: within this loop 5 | for (int c =3D 0; c < 11; c++) | ^~~ Many things are strange about this warning: - First, I think it's invalid -- the "?:" ensures the array indexes are in bounds, all variables are initialized,so I don't see what would be UB. - It claims UB occurs on iteration 7, but the warning disappears when I cha= nge the loop condition to "c < 10" (which would also include iteration 7) - The "if" statement with both conditions and the assignment is necessary to trigger the warning, although it has no actual effect. - The warning seems to apply to the 2nd array index, but already disappears when I turn the first index into "0" without changing the 2nd one. - It disappears when I make all variables local or all global.=