From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 8FE533940CF0; Fri, 13 Nov 2020 18:16:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8FE533940CF0 From: "jim at meyering dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/97817] New: -Wformat-truncation=2 elicits invalid warning Date: Fri, 13 Nov 2020 18:16:53 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jim at meyering dot net 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: Fri, 13 Nov 2020 18:16:53 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97817 Bug ID: 97817 Summary: -Wformat-truncation=3D2 elicits invalid warning Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: jim at meyering dot net Target Milestone: --- Here's the invalid warning. The buffer's size is obviously not 6. It is AT LEAST 6. $ gcc -Wformat-truncation=3D2 -O2 -c strerror_r.c strerror_r.c: In function =E2=80=98strerror_r=E2=80=99: strerror_r.c:12:35: warning: =E2=80=98Unknown error =E2=80=99 directive out= put truncated writing 14 bytes into a region of size 6 [-Wformat-truncation=3D] 12 | snprintf (buf, buflen, "Unknown error %d", errnum); | ~~~~~~^~~~~~~~ strerror_r.c:12:5: note: =E2=80=98snprintf=E2=80=99 output between 16 and 2= 6 bytes into a destination of size 6 12 | snprintf (buf, buflen, "Unknown error %d", errnum); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here's the reduced test case (from gnulib's strerror.c): $ cat strerror_r.c #define size_t unsigned long long extern int snprintf (char *__restrict __s, size_t __maxlen, const char *__restrict __format, ...) __attribute__ ((__format__ (__printf__, 3, 4))); extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen); int strerror_r (int errnum, char *buf, size_t buflen) { if (buflen <=3D 5) return 9; int ret =3D __xpg_strerror_r (errnum, buf, buflen); if (ret =3D=3D 1 && !*buf) snprintf (buf, buflen, "Unknown error %d", errnum); return ret; }=