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

John Lindgren <john at jlindgren dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john at jlindgren dot net

--- Comment #11 from John Lindgren <john at jlindgren dot net> ---
I think I am hitting the same issue in 8.1.0.  Input ranges are thoroughly
checked in the below code, and there is no possibility of truncation, yet a
warning is emitted.

>From reading this and other reports, it seems to come down to random chance
whether -Wformat-truncation is reliable or not, as it depends on range data in
the optimizer that was never intended for error-checking diagnostics.

Can you please remove -Wformat-truncation from -Wall until it actually works?

#include <stdio.h>

void format_time (char buf[7], int time)
{
    if (time < 0) time = 0;
    if (time > 3599999) time = 3599999;

    if (time < 6000)
        snprintf (buf, 7, " %02d:%02d", time / 60, time % 60);
    else if (time < 60000)
        snprintf (buf, 7, "%3d:%02d", time / 60, time % 60);
    else
        snprintf (buf, 7, "%3d:%02d", time / 3600, time / 60 % 60);
}

$ gcc -Wall -O2 -c test.c
test.c: In function ‘format_time’:
test.c:11:32: warning: ‘%02d’ directive output may be truncated writing 2 bytes
into a region of size between 1 and 3 [-Wformat-truncation=]
         snprintf (buf, 7, "%3d:%02d", time / 60, time % 60);
                                ^~~~
test.c:11:27: note: directive argument in the range [0, 59]
         snprintf (buf, 7, "%3d:%02d", time / 60, time % 60);
                           ^~~~~~~~~~
test.c:11:9: note: ‘snprintf’ output between 7 and 9 bytes into a destination
of size 7
         snprintf (buf, 7, "%3d:%02d", time / 60, time % 60);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to