Re: No sscanf Expected Type Warnings When Used Through A Template

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

 



On 13 November 2015 at 09:31, Thomas Thorne <tafthorne@xxxxxxxxx> wrote:
> Good Morning,
>
> I have come across something that I feel a shortcoming in the warnings
> that gcc issues when compiling some C++ code.  Calling it a bug seems a
> bit harsh so I thought I would ask this mailing list of their opinion
> before raising an issue.  When I use a template to perform some sscanf
> work on a string I loose some of the -Wformat warnings about the
> expected argument types.
>
> Here is a set of C++ code that demonstrates the warnings occurring for
> sscanf and not happening for the template.

This has nothing to do with templates, you get exactly the same
behaviour (i.e. no warnings) if you replace the function template
with:

bool message_scanner(const char *message, const char *format, short
int* i, char* c, float* f)
{
  size_t num_scanned(sscanf(message,format,i, c, f));
  bool got_them_all(num_scanned == 3);
  return got_them_all;
}

The problem is simply that the compiler can't check the call to sscanf
unless the first argument is a string literal.

You should be able to solve the problem by adding
__attribute__((format(scanf, blah blah))) to your template function,
which tells the compiler it requires a string literal that meets the
scanf format rules. Then calls to your own sccanf wrapper can be
checked (assuming you pass a string literal to the wrapper).

The format attribute is documented at
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html



[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