Versions Compared

Key

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

The variable parameters of a variadic function, that is, those that correspond with the position of the 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 rule EXP36-C. Do not convert pointers into more strictly aligned pointer types.)

...

  • Integer arguments of types ranked lower than int are promoted to int, if int can hold all the values of that type; otherwise, they are promoted to unsigned int (the "integer promotions").
  • Arguments of type float are promoted to double.

Noncompliant Code Example (Type Interpretation Error)

The C99 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 will likely cause the program to abnormally terminate. Note that the error_message pointer is likewise interpreted as an integer.

Code Block
bgColor#ffcccc
langc

const char *error_msg = "Error occurred";
/* ... */
printf("%s:%d", 15, error_msg);

...

Code Block
bgColor#ccccff
langc

const char *error_msg = "Error occurred";
/* ... */
printf("%d:%s", 15, error_msg);

...

Code Block
bgColor#ffcccc
langc

long long a = 1;
const char msg[] = "Default message";
/* ... */
printf("%d %s", a, msg);

...

Code Block
bgColor#ccccff
langc

long long a = 1;
const char msg[] = "Default message";
/* ... */
printf("%lld %s", a, msg);

Noncompliant Code Example (NULL)

Because C99 allows Because the C standard allows NULL to be either an integer constant or a pointer constant, any architecture where 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 *). Consequently, variadic 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);

...

Code Block
bgColor#ccccff
langc

char* string = NULL;
printf("%s %d\n", (string ? string : "null"), 1);

...

sectionwarns

Warns about inconsistently typed arguments to formatted output functions when the -Wall is used.

Compass/ROSE

does

Does NOT currently detect violations of this recommendation. While the recommendation in general cannot be automated, due to the difficulty in enforcing contracts between a variadic function and its invokers, it would be fairly easy to enforce type correctness on arguments to the printf() family of functions.

section

Tool

Version

Checker

Description

GCC

Include Page
GCC_V
GCC_V

 

Section
Section

 

 

Section

LDRA tool suite

Include Page
LDRA_V
LDRA_V
section

41 S

section

Partially

Implemented

implemented.

Related Vulnerabilities

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

Related Guidelines

ISO/IEC 9899:19992011 Section 6.5.2.2, "Function calls," and Section 7.1516, "Variable arguments"

ISO/IEC TR 24772 "IHN Type system" and "OTR Subprogram Signature Mismatchsignature mismatch"

MISRA Rule 16.1

Bibliography

...