I am getting multiple varFuncNullUB warnings from my static analyzer when calling a variadic function. The warning points to the NULL argument used as a sentinel.
Example:
static inline int EXAMPLE(char *LineStr, int NoOfWords, ...)
{
/* ... */
}
...
EXAMPLE(line, 0, "typedef", "unsigned", "int", NULL);
The analyzer reports:
warning: Passing NULL after the last typed argument to a variadic function leads to undefined behaviour. [varFuncNullUB]
I found suggestions online to cast NULL to const char *:
EXAMPLE(line, 0,
"typedef",
"unsigned",
"int",
(const char *)NULL);
My questions are:
Why does passing plain
NULLto a variadic function trigger this warning?Is casting
NULLtoconst char *the correct and portable solution?If so, why does the cast make a difference?
For context, the variadic arguments are expected to be const char * values terminated by a NULL sentinel.