Re: Strange warning on printf checks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 31 May 2020 at 20:03, NightStrike via Gcc-help
<gcc-help@xxxxxxxxxxx> 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."



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux