Does any of the C Standards describe any kind of a check to ensure that const is used and accessed appropriately with variable argument lists?
If I have the following source for a function with a variable argument list:
int func (char *pBuffer, int nLen, ...)
{
va_list ArgList;
va_start(ArgList, nLen);
char *pName = va_arg(ArgList, char*);
// do things with the string such as copy it to an output buffer.
strcpy (pBuffer, pName); // works. source string argument declared const char *, not modified by strcpy()
*pName = 'x'; // error here running in Debug mode, write access violation.
va_end(ArgList);
return 0;
}
And I want to call this function with a const argument as in:
int func2(char *pBuff, int nLen, const char *pName)
{
return func(pBuff, nLen, pName);
}
I have tried this with Visual Studio 2019 Community Edition and it compiles with no warnings on Warning level 3 using the C11 standard.
When I try to run a Debug compile, I see a write violation exception thrown. When I try a Release compile it runs but the exit code is wrong probably because something important was trampled.
It appears in an AI created source example that the argument pName in the above function, func() could be accessed with const char *p = va_arg(ArgList, const char*); to enforce const however not doing so does not appear to make any difference to the compiler.
So it appears from my testing that using a const variable as a parameter to a function with a variable argument list depends on the called function to be conservative and treat variables as const unless the argument is designated to be non-const by the function documentation.
If the called function does not specify the const modifier in the va_arg() call and the variable definition used with the va_arg() and the called function then tries to modify the variable which may be const then is it Undefined Behavior?
I suppose that vsprintf() takes such a conservative stance with strings and only modifies the output buffer treating the variables in the list of arguments to print as being const?
So it appears that when using variable argument lists a coding policy would be to use const char *p = va_arg(ArgList, const char*); by default unless the argument is explicitly designed to have a modifiable value.
And it doesn't hurt to specify const in other function interfaces even if the leaf function called is using variable arguments. I suppose in many cases function higher up in the call tree may never know the leaf function and whether it uses variable argument list or not.
but the exit code is wrong probably because something important was trampledI used VS 2022 Community, and in Release mode, I got an exit code that was correct. I am curious as to what your exit code was.chararrays andcharpointers to constant strings then exercised the functions. So the source was not all in a single file and there were other C source files as this is a Visual Studio 2019 Community Edition solution with multiple different test and exploratory samples. When I'm investigating some C or C++ question, I just add a new file to the solution that has the source for the investigation and an entry point called from the main.