public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Strange warning on printf checks
@ 2020-05-31 18:32 NightStrike
  2020-05-31 18:35 ` NightStrike
  2020-05-31 20:28 ` Jonathan Wakely
  0 siblings, 2 replies; 3+ messages in thread
From: NightStrike @ 2020-05-31 18:32 UTC (permalink / raw)
  To: gcc-help

I'm curious if this is a gcc bug or not.  The warning I get is trying to
highlight a real problem, but it's referring to a string literal as a
directive, which I thought was just for the %XX printf commands.  Given the
following:

#include <stdio.h>

void f() {
    char x[4];
    char y[5];
    sprintf(x, "%s_%s", y, y);
}

$ gcc a.c -c -Wall
a.c: In function 'f':
a.c:6:16: warning: '_' directive writing 1 byte into a region of size
between 0 and 4 [-Wformat-overflow=]
    6 |  sprintf(x, "%s_%s", y, y);
      |                ^
a.c:6:2: note: 'sprintf' output between 2 and 10 bytes into a destination
of size 4
    6 |  sprintf(x, "%s_%s", y, y);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~


I hope the fixed width display shows this correctly.  The point is that the
^ points to the underscore, which is right, but the message calls the
underscore a printf directive.

Maybe I'm splitting hairs, but this confused me for a good half hour before
I realized what was wrong.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange warning on printf checks
  2020-05-31 18:32 Strange warning on printf checks NightStrike
@ 2020-05-31 18:35 ` NightStrike
  2020-05-31 20:28 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: NightStrike @ 2020-05-31 18:35 UTC (permalink / raw)
  To: gcc-help

Frustratingly, using snprintf doesn't silence the warning.  In my actual
case, both X and Y are chars of size MAXPATHLEN.

On Sun, May 31, 2020 at 2:32 PM NightStrike <nightstrike@gmail.com> wrote:

> I'm curious if this is a gcc bug or not.  The warning I get is trying to
> highlight a real problem, but it's referring to a string literal as a
> directive, which I thought was just for the %XX printf commands.  Given the
> following:
>
> #include <stdio.h>
>
> void f() {
>     char x[4];
>     char y[5];
>     sprintf(x, "%s_%s", y, y);
> }
>
> $ gcc a.c -c -Wall
> a.c: In function 'f':
> a.c:6:16: warning: '_' directive writing 1 byte into a region of size
> between 0 and 4 [-Wformat-overflow=]
>     6 |  sprintf(x, "%s_%s", y, y);
>       |                ^
> a.c:6:2: note: 'sprintf' output between 2 and 10 bytes into a destination
> of size 4
>     6 |  sprintf(x, "%s_%s", y, y);
>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> I hope the fixed width display shows this correctly.  The point is that
> the ^ points to the underscore, which is right, but the message calls the
> underscore a printf directive.
>
> Maybe I'm splitting hairs, but this confused me for a good half hour
> before I realized what was wrong.
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange warning on printf checks
  2020-05-31 18:32 Strange warning on printf checks NightStrike
  2020-05-31 18:35 ` NightStrike
@ 2020-05-31 20:28 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2020-05-31 20:28 UTC (permalink / raw)
  To: NightStrike; +Cc: gcc-help

On Sun, 31 May 2020 at 20:03, NightStrike via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> I'm curious if this is a gcc bug or not.  The warning I get is trying to
> highlight a real problem, but it's referring to a string literal as a
> directive, which I thought was just for the %XX printf commands.  Given the
> following:
>
> #include <stdio.h>
>
> void f() {
>     char x[4];
>     char y[5];
>     sprintf(x, "%s_%s", y, y);
> }
>
> $ gcc a.c -c -Wall
> a.c: In function 'f':
> a.c:6:16: warning: '_' directive writing 1 byte into a region of size
> between 0 and 4 [-Wformat-overflow=]
>     6 |  sprintf(x, "%s_%s", y, y);
>       |                ^
> a.c:6:2: note: 'sprintf' output between 2 and 10 bytes into a destination
> of size 4
>     6 |  sprintf(x, "%s_%s", y, y);
>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> I hope the fixed width display shows this correctly.  The point is that the
> ^ points to the underscore, which is right, but the message calls the
> underscore a printf directive.
>
> Maybe I'm splitting hairs, but this confused me for a good half hour before
> I realized what was wrong.

That is the correct term. The %s parts are conversion specifications,
the "_" between them is a directive.

See https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html

"The format string is composed of zero or more directives: ordinary
characters (not %), which are copied unchanged to the output stream;
and conversion specifications, each of which results in fetching zero
or more subsequent arguments."

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-05-31 20:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-31 18:32 Strange warning on printf checks NightStrike
2020-05-31 18:35 ` NightStrike
2020-05-31 20:28 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).