From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B10DB3858D28; Wed, 15 Dec 2021 17:12:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B10DB3858D28 From: "msebor at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug other/103736] snprintf bogus format-truncation, disregarding modulo on argument Date: Wed, 15 Dec 2021 17:12:37 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: other X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org 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: cc 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 17:12:37 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103736 Martin Sebor changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |msebor at gcc dot gnu.org --- Comment #1 from Martin Sebor --- The warning is based on the annotated IL below. The duplicate modulo computation is optimized to take place just once (in PRE), before the negativity test, the range of the result in the else branch is greater than= the code permits. void func () { char timezone[4]; signed char timezoneval.0_1; int _4; int _12; signed char _13; [local count: 1073741824]: timezoneval.0_1 =3D timezoneval; _13 =3D timezoneval.0_1 % 100; _12 =3D (int) _13; >>> _12's range is [-99, 99] if (timezoneval.0_1 < 0) goto ; [41.00%] else goto ; [59.00%] [local count: 440234144]: _4 =3D -_12; _4's range is [0, 99] snprintf (&timezone, 4, "-%02d", _4); so no warning here goto ; [100.00%] [local count: 633507681]: snprintf (&timezone, 4, "+%02d", _12); << -Wformat-truncation [local count: 1073741824]: timezone =3D{v} {CLOBBER}; return; } The warning can be avoided by using a local temporary copy of the extern variable, like so int x =3D timezoneval; if(x < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-(x % 100)); } else { snprintf(timezone, sizeof(timezone),"+%02d", x % 100); } or simply: int x =3D timezoneval % 100; if(x < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-x); } else { snprintf(timezone, sizeof(timezone),"+%02d", x); }=