Reputation: 12623
VSCode is complaining about the use of va_start
in the following currently working function.
void _log(int level, const char *format, ...)
{
va_list arglist;
va_start(arglist, format);
writelog(level, format, arglist);
}
After searching around, I found a reference to the standard that appears to indicate VSCode is correct and the code will result in undefined behavior.
18.10/3 ...The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.
I tried to find examples of how others handled similar functionality. I found several blogs and several code samples recreating printf
, using implementations similar to the one above. Are these examples also incorrect?
What is the appropriate way to write a printf
-like function without resulting in undefined behavior?
Upvotes: 2
Views: 1384
Reputation: 71
I know this is a 5 years old question, this reply is only for who run in to this issue in the future. I'm using latest version of VS Code at the time of posting btw.
The issue is likely caused by incorrect settings in c_cpp_properties, if you don't have one in your project, then it will use the default. You need set compilerPath and intelliSenseMode to gcc instead of clang.
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
Upvotes: 1
Reputation: 21
So I'm running into this too and it confuses me when working with build output. I believe its a bug (I've registered one here: https://github.com/Microsoft/vscode-cpptools/issues/1720).
I found a maybe horrible work around using special preprocessor logic for __INTELLISENSE__
builds (necessary if a platform can't support Intellisense natively, but vscode is cross platform so....).
Here's the workaround:
#if __INTELLISENSE__
#undef va_start(arg, va)
#define va_start(arg, va)
#undef va_end(va)
#define va_end(va)
#undef va_copy(va0, va1)
#define va_copy(arg0, va1)
#define __INT_MAX__ 0x7fffffff
#endif
I had issues with the definition of __INT_MAX__
as well.
If you're desperate this will get these errors out of the way.
Hopefully someone will figure out an actual solution, or at least one that doesn't require custom code.
Thanks, Adrian
Upvotes: 2