Versions Compared

Key

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

The parameters of a variadic function are interpreted by the va_arg() macro. The va_arg() macro is used to pull extract the next argument from an initialized argument list , the within the body of a variadic function implementation. The size of which each parameter is determined by the parameterized specified type. If type is inconsistent with what was the original parameter to the variadic functionthe corresponding argument, the behavior is undefined and may result in misinterpreted data or an alignment error (see EXP36-C. Do not cast between pointers between objects or types with differing alignments) could result.Therefore, correct typing must always be maintained when using variadic functions

Because arguments to variadic functions are not type, the programmer is responsible for ensuring that arguments to variadic functions are of the same type as the corresponding parameter except for the following cases:

  • one type is a signed integer type, the other type is the corresponding unsigned integer type, and the value is representable in both types;
  • one type is pointer to void and the other is a pointer to a character type.

Non-Compliant Code Example 1

For example, some C99 functions, such as The C99 printf(), are function is implemented as variadic functions, if care is not taken to ensure that the conversion specifiers to these do not match up with the type of the intended parameter, the result may be abnormal program termination or misinterpreted data.The following . This non-compliant code example swaps its null terminated byte string and integer parameters with respect to how they were specified in the format string. This means that Consequently, the integer is silently casted converted into a pointer to a null terminated byte string and then dereferenced, possibly causing the program to abnormally terminate (error_message pointer is likewise silently converted into an integer).

...

Non-Compliant Code Example 2

In the following this non-compliant code example, a type long long integer is parsed by the printf() function with just a %d specifier, resulting in data truncation or misrepresentation when the value is pulled from the argument list.

...