...
- SPACE—the space (
" "
) character - N/E—no effect
- NTBS—
char*
argument pointing to a null-terminated byte string - NTWS—
wchar_t*
argument pointing to a null-terminated wide character string - XSI—ISO/IEC 9945-2003 XSI extension
Noncompliant Code Example
Mismatches between arguments and conversion specifications may result in undefined behavior. Many compilers can diagnose type mismatches in formatted output function invocations. In the following noncompliant code example, the error_type
argument to printf()
is incorrectly matched with the %s
specifier (should be %d
), and the error_msg
argument is incorrectly matched with the %d
specifier (should be %s
). One possible result of this invocation is that printf()
will interpret the error_type
argument as a pointer, and try to read a string from the address that error_type
contains (likely this will result in an access violation):
Code Block | ||||
---|---|---|---|---|
| ||||
const char *error_msg = "Resource not available to user."; int error_type = 3; /* ... */ printf("Error (type %s): %d\n", error_type, error_msg); |
Compliant Solution
This compliant solution ensures that the format arguments match their respective format specifications:
Code Block | ||||
---|---|---|---|---|
| ||||
const char *error_msg = "Resource not available to user."; int error_type = 3; /* ... */ printf("Error (type %d): %s\n", error_type, error_msg); |
Noncompliant Code Example
The width and precision arguments to printf()
format directives must be of type int
. Subclause 7.21.6.1 of the C Standard [ISO/IEC 9899:2011] states:
...
Code Block | ||||
---|---|---|---|---|
| ||||
int print_int(int i, size_t width, size_t prec) { int n; n = printf("%*.*d", width, prec, i); return n; } |
Compliant Solution
In this compliant solution, the field width and precision arguments to printf()
format directives are of type int
:
Code Block | ||||
---|---|---|---|---|
| ||||
int print_int(int i, int width, int prec) { int n; n = printf("%*.*d", width, prec, i); return n; } |
Risk Assessment
In most cases, incorrectly specified format strings will result in abnormal program termination.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO00-C | High | Unlikely | Medium | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
| Can detect violations of this recommendation when the | |||||||
| SV.FMT_STR |
| |||||||
| 486 S | Fully implemented | |||||||
PRQA QA-C |
| 0179 (U) | Partially implemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
CERT C++ Secure Coding Standard | FIO00-CPP. Take care when creating format strings |
ISO/IEC TS 17961 | Using invalid format strings [invfmtstr] |
MITRE CWE | CWE-686, Function call with incorrect argument type |
Bibliography
[ISO/IEC 9899:2011] | Subclause 7.21.6.1, "The fprintf Function" |
...