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

            Bug ID: 103352
           Summary: Wrong computation of -Wformat-truncation=2
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: julien at trigofacile dot com
  Target Milestone: ---

When building
https://raw.githubusercontent.com/InterNetNews/inn/main/lib/timer.c I have a
warning with -Wformat-truncation=2

    len = 52 * timer_count + 27 + (prefix == NULL ? 0 : strlen(prefix)) + 1;
    buf = xmalloc(len);
    if (prefix == NULL)
        rc = 0;
    else
        rc = snprintf(buf, len, "%s ", prefix);

timer.c: In function ‘TMRsummary’:
timer.c:395:36: warning: ‘ ’ directive output may be truncated writing 1 byte
into a region of size between 0 and 1 [-Wformat-truncation=]
  395 |         rc = snprintf(buf, len, "%s ", prefix);
      |                                    ^
timer.c:395:14: note: ‘snprintf’ output 2 or more bytes (assuming 3) into a
destination of size 1
  395 |         rc = snprintf(buf, len, "%s ", prefix);
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


It seems that "len" is considered to be of length 1.  I bet it comes from the
"+ 1" at the end of the declaration of "len".
If I move the "+ 27" at the end of the declaration of "len", the warning
disappears.

Isn't there something to improve in the way the computation is done for that
warning?

Thanks beforehand.
  • [Bug c/103352] New: Wrong compu... julien at trigofacile dot com via Gcc-bugs

Reply via email to