Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The variable parameters of a variadic function, that function—that is, those that correspond with the position of the ellipsis, are ellipsis—are interpreted by the va_arg() macro. The va_arg() macro is used to extract the next argument from an initialized argument list within the body of a variadic function implementation. The size of each parameter is determined by the specified type. If the type is inconsistent with the corresponding argument, the behavior is undefined and may result in misinterpreted data or an alignment error. (See EXP36-C. Do not convert cast pointers into more strictly aligned pointer types.)

The variable arguments to a variadic function are not checked for type by the compiler. As a result, the programmer is responsible for ensuring that they are compatible with the corresponding parameter after the default argument promotions:

...

The C printf() function is implemented as a variadic function. This noncompliant code example swaps its null-terminated byte string and integer parameters with respect to how they are specified in the format string. Consequently, the integer is interpreted as a pointer to a null-terminated byte string and dereferenced. This , which will likely cause the program to abnormally terminate. Note that the error_message pointer is likewise interpreted as an integer.

...

Because the C Standard allows NULL to be either an integer constant or a pointer constant, any architecture where in which int is not the same size as a pointer might present a particular vulnerability with variadic functions. If NULL is defined as an int on such a platform, then sizeof(NULL) != sizeof(void *), so variadic functions that accept an argument of pointer type will not correctly promote NULL to the correct size. Consequently, the following code will have undefined behavior:

Code Block
bgColor#ffcccc
langc
char* string = NULL;
printf("%s %d\n", string, 1);

...

Inconsistent typing in variadic functions can result in abnormal program termination or unintended information disclosure.

...

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this recommendation on the CERT website.

...