2

Say, if I compile the following using Visual Studio 2017 C++ compiler:

int r = 0;
wprintf_s(L"%s", r);

It will give me these very handy warnings:

warning C4477: 'wprintf' : format string '%s' requires an argument of type 'wchar_t *', but variadic argument 1 has type 'int'

warning C4313: 'wprintf': '%s' in format string conflicts with argument 1 of type 'int'

But when I try to define my own variadic function:

void MyFormat(_In_z_ _Printf_format_string_ LPCTSTR pszFormat, ...)
{
    va_list argList;
    va_start( argList, pszFormat );

    //Do work ...

    va_end( argList );
}

and then call it in a similar manner:

int r = 0;
MyFormat(L"%s", r);

It doesn't trigger them.

So I'm wondering if I can enable those warnings for my own variadic function?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 2
    [this](https://godbolt.org/z/N4FWVW) prove that msvc (cl.exe) use hardcoded list of functions for which he check parameters and make warnings. say `sprintf` in this list, but `sprintf2` not – RbMm Feb 28 '19 at 00:46

1 Answers1

2

Things like _In_z_ and _Printf_format_string_ are SAL annotation macros. They are recognized by static analysis tools, but they are removed by the preprocessor before the compiler ever sees them. So they are not very useful in your situation.

Some 3rd party compilers implement vendor-specific ways to enable compile-time validation of printf-style parameters on user-defined functions (such as __attribute__(format) and __attribute__(format_arg) in GCC), however Visual C++ is not one of those compilers (see __attribute__((format(printf, 1, 2))) for MSVC?). The VC++ team chose to enable compile-time validations for only the standard printf/scanf family of C runtime functions, as documented on their blog in 2015:

C++ Team Blog: Format Specifiers Checking

By popular request, in Visual Studio 2015 RTM, we’ve implemented the checking of arguments given to printf/scanf and their variations in the C standard library. You can try the examples from this post in our online compiler.

...

Currently, the checking of format specifiers is only done for a predefined set of CRT functions and is not available for user-defined functions that would also benefit from similar checks. If there is enough interest, we will consider extending these warnings to work on such user-defined functions.

If you really want compile-time checking of user-defined variadic functions, use variadic templates instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • no, SAL annotation not play any role - look for this example - https://godbolt.org/z/N4FWVW - no any annotation here, but.. – RbMm Feb 28 '19 at 00:48
  • @RbMm I know SAL doesn't play a role, that was kind of my point – Remy Lebeau Feb 28 '19 at 00:56
  • but even without any attributes msvc (cl.exe) produce such warnings - just another example - https://godbolt.org/z/5KRz5X - how you explain this ? – RbMm Feb 28 '19 at 00:56
  • i think that exist hardcoded list of names, for wich msvc make this check. not any attributes but hardcoded list – RbMm Feb 28 '19 at 00:57
  • @RbMm I updated my answer with a direct quote from MS that backs up your assumption. – Remy Lebeau Feb 28 '19 at 01:04
  • if interesting - [this](https://pastebin.com/Ne2RtZEQ) is current list of this functions and [here](https://prnt.sc/mr47ai) (inside c1xx.dll) was check – RbMm Feb 28 '19 at 01:23