From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3DD713858D39; Sun, 12 Dec 2021 23:14:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DD713858D39 From: "robert.muench at saphirion dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/103670] Incorrect optimization of loop termination: Early exit with any optimization Date: Sun, 12 Dec 2021 23:14:13 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 10.3.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: robert.muench at saphirion dot com X-Bugzilla-Status: WAITING 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: 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: Sun, 12 Dec 2021 23:14:13 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103670 --- Comment #2 from Robert M. M=C3=BCnch --- That's the code snippet. The for(...) loop is where things start to become strange.=20 The thing is, if we use the code from a pure C program, it works. We are us= ing this code from a Windows DLL which we call via an FFI from an interpreter. However, omitting any optimization makes it work. if (pAdapter !=3D NULL) { int i =3D 0; int offset =3D 0; // loop for as many bytes as given by the address length // sprintf returns num char written, take: out buffer, max size of buffer, format, ... // format: %[flags][width][.precision][size]type // .2 =3D two characters // type X =3D Unsigned hexadecimal integer; uses "ABCDEF" // %.2X=20 LOG(" AddressLenth: %i", pAdapter->AddressLength); // MAC-48 =3D 48 bit, 6 bytes, 6 two char blocks LOG(" mac: %.12X", pAdapter->Address); for (i =3D 0; i < pAdapter->AddressLength; i++) { LOG(" i: %i offset: %i, AddressLenth: %i", i, offset, pAdapter->AddressLength); // omit outputting the - char for the last byte if (i =3D=3D (pAdapter->AddressLength - 1)) { LOG("%s", " finalizing MAC address buidling"); offset +=3D sprintf_s(MacAddress + offset, MAX_ADAPTER_ADDRESS_LENGTH, "%02X",pAdapter->Address[i]); } else { offset +=3D sprintf_s(MacAddress + offset, MAX_ADAPTER_ADDRESS_LENGTH, "%02X-",pAdapter->Address[i]); } } LOG("<- OK: %s AddressLength: %i", MacAddress, i); return MacAddress; }=