From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B815D385842D; Wed, 15 Dec 2021 14:39:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B815D385842D From: "patrickdepinguin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug other/103736] New: snprintf bogus format-truncation, disregarding modulo on argument Date: Wed, 15 Dec 2021 14:39:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new 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: patrickdepinguin at gmail dot com 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 14:39:44 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103736 Bug ID: 103736 Summary: snprintf bogus format-truncation, disregarding modulo on argument Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: patrickdepinguin at gmail dot com Target Milestone: --- gcc 11.2.0 and gcc 9.4.0 give a bogus format-truncation warning on following test case compiled with -Wall and -O2: #include #include void func(void) { extern int8_t timezoneval; char timezone[1+2+1]; if(timezoneval < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-(timezoneval % 100)); } else { snprintf(timezone, sizeof(timezone),"+%02d", timezoneval % 100); } } Warning: /tmp/test.cpp: In function 'void func()': /tmp/test.cpp:15:52: warning: 'snprintf' output may be truncated before the last format character [-Wformat-truncation=3D] 15 | snprintf(timezone, sizeof(timezone),"+%02d", timezoneval % 100); | ^ /tmp/test.cpp:15:18: note: 'snprintf' output between 4 and 5 bytes into a destination of size 4 15 | snprintf(timezone, sizeof(timezone),"+%02d", timezoneval % 100); |=20=20=20=20=20=20=20=20=20 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since timezoneval is used modulo 100, it will not take up more than two dig= its (note that if timezoneval is negative, its value is negated first, so the string representation will be positive). Together with the literal sign character, and the null-termination, max. total size is 4 bytes. Yet, gcc considers that 5 bytes may be needed. When the parentheses in the first snprintf are omitted, causing the modulo operator to operate on the negated timezoneval, the warning disappears. Fun= nily enough, the warning is about the _second_, unmodified, snprintf: #include #include void func(void) { extern int8_t timezoneval; char timezone[1+2+1]; if(timezoneval < 0) { snprintf(timezone, sizeof(timezone),"-%02d",-timezoneval % 100); } else { snprintf(timezone, sizeof(timezone),"+%02d", timezoneval % 100); } } I found some possibly related older bugs, but was unsure if it's the same a= nd known to still apply on gcc 11. Feel free to mark this one as duplicated to= the relevant one. Bug #78969 - bogus snprintf truncation warning due to missing range info Bug #77721 - -Wformat-truncation not uses arg range for converted vars=20 Bug #94021 - -Wformat-truncation false positive due to excessive integer ra= nge=