Why does passing NULL to a variadic function trigger a static analyzer warning about undefined behavior?
04:13 03 Aug 2026

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:

  1. Why does passing plain NULL to a variadic function trigger this warning?

  2. Is casting NULL to const char * the correct and portable solution?

  3. 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.

c variadic cppcheck