6

In the following snippet the wrong usage of format specifiers inside the MyFormat() call should produce a warning, according to SAL specifications, and if I uncomment the identical call of printf(), I really will receive all these warnings, but my code is compiled silently even with /W4. What am I doing wrong? I'm using MSVC 2017 15.9.7 Community edition.

#include <stdio.h>
#include <stdarg.h>

void MyFormat(_Printf_format_string_ const char *fmt, ...)
{
   va_list va;
   va_start(va, fmt);
   vprintf(fmt, va);
   va_end(va);
}

int main()
{
   MyFormat("blabla %s\n", L"qq");
   // printf("blabla %s\n", L"qq");
   return 0;
}
George Hazan
  • 170
  • 1
  • 9
  • I suspect that you have C++ code analysis disabled. For me this causes `warning C6303: Format string mismatch: wide character string passed as _Param_(2) when character string is required in call to 'MyFormat' Actual type: 'const wchar_t [3]'.` – user7860670 Feb 22 '19 at 16:09
  • but why does it print a warning for printf then? I don't use VS, I just feed this small file to cl.exe – George Hazan Feb 22 '19 at 21:01
  • C++ code analysis adds a lot of other controls and slow down the compiling process. on gcc works from 2000, why on 2019 there is no support on visual studio? – Perry Apr 20 '23 at 07:46

2 Answers2

3

Adding the /analyze flag will cause this to produce a warning. However it is a different (and in my opinion inferior) warning than what you would get from printf. Unfortunately I can't find a way to make a user-defined function to produce that style of warning.

David Brown
  • 13,336
  • 4
  • 38
  • 55
0

Just went through this headscratcher myself, and for me it was because the warnings were all explicitly disabled. (eg 6340, 6284, 6273.) printf emits different warnings (4477) which were not disabled.

Without seeing OP's full command-line, it's impossible to know if that was their problem, but to anyone else going through this: check your /wd flags.