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

Matteo Croce <mcroce at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mcroce at redhat dot com

--- Comment #11 from Matteo Croce <mcroce at redhat dot com> ---
Hi,

when compiling this snippet:

        void f()
        {
                const char *dir = "a";
                const char file[50] = "b";
                char buf[4];
                snprintf(buf, sizeof(buf), "%s/%s", dir, file);
        }

I get:

buf.c: In function ‘f’:
buf.c:19:33: warning: ‘%s’ directive output may be truncated writing up to 49
bytes into a region of size 3 [-Wformat-truncation=]
   19 |  snprintf(buf, sizeof(buf), "%s/%s", dir, file);
      |                                 ^~        ~~~~
buf.c:19:2: note: ‘snprintf’ output 2 or more bytes (assuming 51) into a
destination of size 4
   19 |  snprintf(buf, sizeof(buf), "%s/%s", dir, file);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


While I agree with the first warning (directive output may be truncated writing
up to 49 bytes into a region of size 3) I'm not fully convinced about
"‘snprintf’ output 2 or more bytes (assuming 51) into a destination of size 4".
snprintf will output up to 4 bytes, NULL terminator included according to the
documentation:

        The functions snprintf() and vsnprintf() write at most size bytes
        (including the terminating null byte ('\0')) to str.

I'm using GCC 9.1.1

Regards,
Matteo Croce

Reply via email to