[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2022-03-17 Thread msebor at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

Martin Sebor  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|ASSIGNED|RESOLVED

--- Comment #5 from Martin Sebor  ---
GCC 11 and 12 print the following for the test case in comment #0.  I believe
the problem has been resolved (the %G format is gone now from 12).

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

[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2018-01-03 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

Martin Sebor  changed:

   What|Removed |Added

 CC||sylvestre at debian dot org

--- Comment #4 from Martin Sebor  ---
*** Bug 83586 has been marked as a duplicate of this bug. ***

[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2017-12-15 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

Martin Sebor  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |msebor at gcc dot 
gnu.org

[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2017-12-15 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

Martin Sebor  changed:

   What|Removed |Added

   Keywords||diagnostic
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2017-12-15
 CC||msebor at gcc dot gnu.org
   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=83369,
   ||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=83336
 Ever confirmed|0   |1

--- Comment #3 from Martin Sebor  ---
Confirmed.  The whole sprintf pass is missing the %G directive in warning_at()
calls to print the inlining context.  With it added the warning for the test
case from comment #2 looks like this:

d.c: In function ‘void test(S*)’:
d.c:16:17: warning: ‘%s’ directive argument is null [-Wformat-truncation=]
 snprintf(s->out, sizeof(s->out), ".%s", s->str);
 ^~~

This should be easy to fix.  It's been in the back of my mind to do this but it
hasn't risen up to the top of the priority list.  With a bug reported for it I
have more of an incentive -- thanks :)

For reference, pr83369 and pr83336 point out similar or related problems with
the inlining context.

[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2017-12-15 Thread bugzi...@poradnik-webmastera.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

--- Comment #2 from Daniel Fruzynski  ---
Sometimes actual location is not reported at all:

[code]
#include 
#include 

struct S
{
char* str;
int n;
char out[10];
};

void test(S* s)
{
if (s->str)
snprintf(s->out, sizeof(s->out), "%d", s->n);
else
snprintf(s->out, sizeof(s->out), ".%s", s->str);
}
[/code]

[out]
test.cc: In function ‘void test(S*)’:
test.cc:11:6: warning: ‘%s’ directive argument is null [-Wformat-truncation=]
 void test(S* s)
  ^~~~
[/out]

[Bug c++/83429] Incorrect line number reported by -Wformat-truncation

2017-12-15 Thread bugzi...@poradnik-webmastera.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83429

--- Comment #1 from Daniel Fruzynski  ---
Another test case, this time "note:" with argument range also points to
incorrect line:

[code]
#include 

struct S
{
unsigned char n;
char out[2];
};

void test(S* s) // line 9
{
snprintf(s->out, sizeof(s->out), "%d", s->n); // line 11
}
[/code]

[out]
test.cc: In function ‘void test(S*)’:
test.cc:9:6: warning: ‘%d’ directive output may be truncated writing between 1
and 3 bytes into a region of size 2 [-Wformat-truncation=]
 void test(S* s) // line 9
  ^~~~
test.cc:9:6: note: directive argument in the range [0, 255]
test.cc:11:13: note: ‘snprintf’ output between 2 and 4 bytes into a destination
of size 2
 snprintf(s->out, sizeof(s->out), "%d", s->n); // line 11
 ^~~~
[/out]