[Bug c++/77958] printf format checking -vs- variadic template functions

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Oct 12 18:41:00 GMT 2016


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
                 CC|                            |msebor at gcc dot gnu.org
           Severity|normal                      |enhancement

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
It would certainly be nice if it did work, though I think it doesn't simply
because attribute format isn't designed to be used with variadic funtion
templates.  I don't have a sense how involved extending it to those would be
but let me confirm this as an enhancement request.

Btw., it's nice to see that because the -Wfornat-length warning runs later than
-Wformat it handles this case even without the attribute.  Moving -Wformat to a
later stage would have the same effect, in addition to letting it detect more
problems (e.g., when the format string isn't a literal).

$ cat z.C && gcc -S -O2  -Wall -Wextra -Wpedantic z.C
char d [4];

template<typename... Args>
static inline void
whatever (char *d, const char *fmt, Args&&... args)
{
  __builtin_sprintf(d, fmt, args...);
}

void qq()
{
  whatever(d, "hi %s", "bob");
}

z.C: In function ‘void qq()’:
z.C:10:6: warning: ‘%s’ directive writing 3 bytes into a region of size 1
[-Wformat-length=]
 void qq()
      ^~
z.C:7:3: note: format output 7 bytes into a destination of size 4
   __builtin_sprintf(d, fmt, args...);
   ^~~~~~~~~~~~~~~~~


More information about the Gcc-bugs mailing list