Versions Compared

Key

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

The formatted IO functions fprintf(), printf(), sprintf(), snprintf(), vfprintf(), vprintf(), vsprintf(), and vsnprintf() convert, format, and print their arguments under control of a format string. Section  Subclause 7.21.6.1 of the C Standard [ISO/IEC 9899:2011] , states:

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.

...

The following table summarizes C-compliant conversion specifiers along with the flag characters valid for each specification (the apostrophe ([')], -, +, the space character, and # in columns 2 through 5) and length modifiers (h, hh, l, ll, j, z, t, and L in columns 6 through 13) and the type of the expected argument. Valid and meaningful combinations of a conversion specification, flag character, and length modifier is are denoted by the (tick) symbol in the corresponding cell or by the name of the type argument affected by the length modifier. Valid combinations that have no effect are denoted by N/E. Using a combination of a conversion specification, flag character, and length modifier denoted by the (error) symbol or a specification not listed in the table, or using an argument of an unexpected type, may result in undefined behavior. See undefined behaviors 153, 155157, 158, 161, and 162 in Annex J of the C Standard.

...

The width and precision arguments to printf() format directives must be of type int. Section Subclause 7.21.6.1 of the C standard Standard [ISO/IEC 9899:2011] , states:

A field width, or precision, or both, may be indicated by an asterisk ('*'). In this case, an argument of type int supplies the field width or precision.

...

In most cases, incorrectly specified format strings will result in abnormal program termination.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

FIO00-C

highHigh

unlikelyUnlikely

mediumMedium

P6

L2

Automated Detection

Tool

Version

Checker

Description

GCC

Include Page
GCC_V
GCC_V

 

Can detect violations of this recommendation when the -Wformat flag is used

Klocwork

Include Page
Klocwork_V
Klocwork_V

SV.FMT_STR

 

LDRA tool suite

Include Page
LDRA_V
LDRA_V

486 S
589 S

Fully implemented

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0179 (U)
0180 (C99)
0184 (U)
0185 (U)
0190 (U)
0191 (U)
0192 (U)
0193 (U)
0194 (U)
0195 (U)
0196 (U)
0197 (U)
0198 (U)
0199 (U)
0200 (U)
0201 (U)
0202 (I)
0206 (U)

Partially implemented

...

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

Related Guidelines

CERT C++ Secure Coding StandardFIO00-CPP. Take care when creating format strings
ISO/IEC TS 17961 (Draft)Using invalid format strings [invfmtstr]
MITRE CWECWE-686, Function call with incorrect argument type

Bibliography

[ISO/IEC 9899:2011]Section 7.21.6.1, "The fprintf Function"

...