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

            Bug ID: 83429
           Summary: Incorrect line number reported by -Wformat-truncation
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzi...@poradnik-webmastera.com
  Target Milestone: ---

[code]
#include <stdio.h>

struct S
{
    char str1[10];
    char str2[10];
    char out[15];
};

void test(S* s) // line 10
{
    snprintf(s->out, sizeof(s->out), "%s.%s", s->str1, s->str2); // line 12
}
[/code]

When above code is compiles using "g++ -c -o test.o test.cc -O2 -Wall", it
produces following output:

[out]
test.cc: In function ‘void test(S*)’:
test.cc:10:6: warning: ‘%s’ directive output may be truncated writing up to 9
bytes into a region of size between 5 and 14 [-Wformat-truncation=]
 void test(S* s) // line 10
      ^~~~
test.cc:12:13: note: ‘snprintf’ output between 2 and 20 bytes into a
destination of size 15
     snprintf(s->out, sizeof(s->out), "%s.%s", s->str1, s->str2); // line 12
     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/out]

As you can see, line number in "warning:" line is incorrect - it points to line
with function name. Fortunately correct number is in line with "note:". However
when code is compiled with -D_FORTIFY_SOURCE=1 added, you loose this important
piece of information:

[out]
test.cc: In function ‘void test(S*)’:
test.cc:10:6: warning: ‘%s’ directive output may be truncated writing up to 9
bytes into a region of size between 5 and 14 [-Wformat-truncation=]
 void test(S* s) // line 10
      ^~~~
In file included from /usr/include/stdio.h:937,
                 from test.cc:1:
/usr/include/bits/stdio2.h:64:35: note: ‘__builtin_snprintf’ output between 2
and 20 bytes into a destination of size 15
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/out]

g++ --version
g++ (GCC) 8.0.0 20171210 (experimental)

Reply via email to