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

* [Bug c/97817] -Wformat-truncation=2 elicits invalid warning
  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 ` jim at meyering dot net
  2020-11-13 18:37 ` schwab@linux-m68k.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jim at meyering dot net @ 2020-11-13 18:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from jim at meyering dot net ---
I confirmed this happens both with the very latest built from git: gcc version
11.0.0 20201113 (experimental) (GCC), and Fedora 32's gcc version 10.2.1
20201016 (Red Hat 10.2.1-6) (GCC).

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

* [Bug c/97817] -Wformat-truncation=2 elicits invalid warning
  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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: schwab@linux-m68k.org @ 2020-11-13 18:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andreas Schwab <schwab@linux-m68k.org> ---
But when it's 6 it's truncated.

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

* [Bug c/97817] -Wformat-truncation=2 elicits invalid warning
  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
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-11-13 22:36 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
Level 2 of the warning is documented to warn also about calls to bounded
functions whose return value is used and that might result in truncation given
an argument of sufficient length or magnitude.  The level is meant to help
write code with the least likelihood of truncation given unknown arguments.

In the test case, the output of the function will be truncated unless buflen is
at least 16.  It will also be truncated if buflen is 16 and errnum is either
negative or bigger than 9.  The note printed after the warning indicates the
minimum size of output (i.e., 16) and the maximum (26) beyond which truncation
is impossible.

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

* [Bug c/97817] -Wformat-truncation=2 elicits invalid warning
  2020-11-13 18:16 [Bug c/97817] New: -Wformat-truncation=2 elicits invalid warning jim at meyering dot net
                   ` (2 preceding siblings ...)
  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
  4 siblings, 0 replies; 6+ messages in thread
From: jim at meyering dot net @ 2020-11-13 23:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from jim at meyering dot net ---
Thanks for explaining. It would be nice if the diagnostic were to say something
along the lines of "... writing into a region whose size may be as low as N".
Given the wording of the current diagnostic, I initially went looking for a
caller whose buffer really did have length 2 (in the original it was 2, not 6).

It's only when I finally noticed that initial "if" block in the implementation
that I understood where the "2" (6 in this example) was coming from.

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

* [Bug c/97817] -Wformat-truncation=2 elicits invalid warning
  2020-11-13 18:16 [Bug c/97817] New: -Wformat-truncation=2 elicits invalid warning jim at meyering dot net
                   ` (3 preceding siblings ...)
  2020-11-13 23:03 ` jim at meyering dot net
@ 2020-11-18 23:16 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-11-18 23:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
I agree that the text of the warning could be improved.  I'm hoping to make
changes along the lines you suggest for GCC 12 (it's too late for GCC 11),

^ 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).