public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97817] New: -Wformat-truncation=2 elicits invalid warning
@ 2020-11-13 18:16 jim at meyering dot net
  2020-11-13 18:24 ` [Bug c/97817] " jim at meyering dot net
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jim at meyering dot net @ 2020-11-13 18:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97817

            Bug ID: 97817
           Summary: -Wformat-truncation=2 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=2 -O2 -c strerror_r.c
strerror_r.c: In function ‘strerror_r’:
strerror_r.c:12:35: warning: ‘Unknown error ’ directive output truncated
writing 14 bytes into a region of size 6 [-Wformat-truncation=]
   12 |     snprintf (buf, buflen, "Unknown error %d", errnum);
      |                             ~~~~~~^~~~~~~~
strerror_r.c:12:5: note: ‘snprintf’ output between 16 and 26 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 <= 5)
    return 9;
  int ret = __xpg_strerror_r (errnum, buf, buflen);
  if (ret == 1 && !*buf)
    snprintf (buf, buflen, "Unknown error %d", errnum);
  return ret;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-11-18 23:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-13 18:16 [Bug c/97817] New: -Wformat-truncation=2 elicits invalid warning jim at meyering dot net
2020-11-13 18:24 ` [Bug c/97817] " jim at meyering dot net
2020-11-13 18:37 ` schwab@linux-m68k.org
2020-11-13 22:36 ` msebor at gcc dot gnu.org
2020-11-13 23:03 ` jim at meyering dot net
2020-11-18 23:16 ` msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).