Strange warning on printf checks

NightStrike nightstrike@gmail.com
Sun May 31 18:35:08 GMT 2020


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.
>
>


More information about the Gcc-help mailing list