Format string specifiers and arguments mismatch
Format specifiers in printf
-like functions do not match corresponding
arguments
Description
This defect occurs when
the format specifiers in the formatted output functions such as
printf
do not match their corresponding arguments. For example,
an argument of type unsigned long
must have a format specification of
%lu
.
Risk
Mismatch between format specifiers and the corresponding arguments result in undefined behavior.
Fix
Make sure that the format specifiers match the corresponding arguments. For instance, in this example, the %d
specifier does not match the string argument message
and the %s
specifier does not match the integer argument err_number
.
const char *message = "License not available"; int err_number = -4; printf("Error: %d (error type %s)\n", message, err_number);
In cases where integer promotion modifies the perceived data type of an argument, the analysis result shows both the original type and the type after promotion. The format specifier has to match the type after integer promotion.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On |
Command-Line Syntax: STRING_FORMAT |
Impact: Low |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Invalid use of standard library string routine
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)