This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/77721] New: -Wformat-length not uses arg range for converted vars


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

            Bug ID: 77721
           Summary: -Wformat-length not uses arg range for converted vars
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dimhen at gmail dot com
  Target Milestone: ---

gcc version 7.0.0 20160922 (experimental) [trunk revision 240342] (GCC)

no warns in foo() but warns in bar()

$ cat x.c
int snprintf (char*, __SIZE_TYPE__, const char*, ...);

void foo(unsigned j, char *p)
{
    if(j > 999)
        return;
    snprintf(p, 4, "%3u", j);
}

void bar(int j, char *p)
{
    const unsigned k = (unsigned) j;
    if(k > 999)
        return;
    snprintf(p, 4, "%3u", k);
}

$ gcc -Wall -c -O2 x.c
x.c: In function ‘bar’:
x.c:15:21: warning: ‘%3u’ directive output may be truncated writing between 3
and 10 bytes into a region of size 4 [-Wformat-length=]
     snprintf(p, 4, "%3u", k);
                     ^~~
x.c:15:20: note: directive argument in the range [0u, 4294967295u]
     snprintf(p, 4, "%3u", k);
                    ^~~~~
x.c:15:5: note: format output between 4 and 11 bytes into a destination of size
4
     snprintf(p, 4, "%3u", k);
     ^~~~~~~~~~~~~~~~~~~~~~~~

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]