Versions Compared

Key

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

...

As shown, care should be taken that the arguments passed to a format string function match up with the supplied format string.

Non-Compliant Code Example (type alignment error)

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

...

Because a long long was not interpreted, if the architecture is set up in a way that long long uses more bits for storage, the subsequent format specifier %s is unexpectedly offset, causing unknown data to be used instead of the pointer to the message.

Compliant Solution (type alignment error)

This compliant solution adds in the length modifier ll to the %d format specifier so that the variadic function parser for printf() pulls the right amount of space off of the variable argument list for the long long argument.

...