|
4 | 4 | <qhelp>
|
5 | 5 | <overview>
|
6 | 6 | <p>Each call to the <code>printf</code> function or a related function should include
|
7 |
| -the type and sequence of arguments defined by the format. If the function is passed arguments |
| 7 | +the type and sequence of arguments defined by the format. If the function is passed arguments |
8 | 8 | of a different type or in a different sequence then the arguments are reinterpreted to fit the type and sequence expected, resulting in unpredictable behavior.</p>
|
9 | 9 |
|
10 | 10 | </overview>
|
11 | 11 | <recommendation>
|
12 |
| -<p>Review the format and arguments expected by the highlighted function calls. Update either |
13 |
| -the format or the arguments so that the expected type and sequence of arguments are passed to |
| 12 | +<p>Review the format and arguments expected by the highlighted function calls. Update either |
| 13 | +the format or the arguments so that the expected type and sequence of arguments are passed to |
14 | 14 | the function.
|
15 | 15 | </p>
|
16 | 16 |
|
17 | 17 | </recommendation>
|
18 |
| -<example><sample src="WrongTypeFormatArguments.cpp" /> |
| 18 | +<example> |
19 | 19 |
|
20 |
| -</example> |
21 |
| -<references> |
| 20 | +<p>In the following example, the wrong format specifier is given for an integer format argument:</p> |
| 21 | + |
| 22 | +<sample src="WrongTypeFormatArgumentsBad.cpp" /> |
22 | 23 |
|
23 |
| -<li>CERT C Coding |
24 |
| -Standard: <a href="https://www.securecoding.cert.org/confluence/display/c/FIO30-C.+Exclude+user+input+from+format+strings">FIO30-C. Exclude user input from format strings</a>.</li> |
25 |
| -<li>cplusplus.com: <a href="http://www.tutorialspoint.com/cplusplus/cpp_functions.htm">C++ Functions</a>.</li> |
26 |
| -<li>CRT Alphabetical Function Reference: <a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l">printf, _printf_l, wprintf, _wprintf_l</a>.</li> |
| 24 | +<p>The corrected version uses <code>%i</code> as the format specifier for the integer format argument:</p> |
27 | 25 |
|
| 26 | +<sample src="WrongTypeFormatArgumentsGood.cpp" /> |
28 | 27 |
|
| 28 | +</example> |
| 29 | +<references> |
29 | 30 |
|
| 31 | +<li>Microsoft Learn: <a href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-170">Format specification syntax: printf and wprintf functions</a>.</li> |
| 32 | +<li>cplusplus.com:<a href="https://cplusplus.com/reference/cstdio/printf/"></a>printf</li> |
| 33 | +<li>CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/FIO47-C.+Use+valid+format+strings">FIO47-C. Use valid format strings</a>.</li> |
30 | 34 |
|
31 | 35 | </references>
|
32 | 36 | </qhelp>
|
0 commit comments