Skip to content

Commit e22159a

Browse files
committed
C++: Update WrongTypeFormatArguments.qhelp.
1 parent 355c7d9 commit e22159a

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.qhelp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@
44
<qhelp>
55
<overview>
66
<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
88
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>
99

1010
</overview>
1111
<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
1414
the function.
1515
</p>
1616

1717
</recommendation>
18-
<example><sample src="WrongTypeFormatArguments.cpp" />
18+
<example>
1919

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" />
2223

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>&percnt;i</code> as the format specifier for the integer format argument:</p>
2725

26+
<sample src="WrongTypeFormatArgumentsGood.cpp" />
2827

28+
</example>
29+
<references>
2930

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"
34+
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>
3035

3136
</references>
3237
</qhelp>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
int main() {
2-
printf("%s\n", 42); //printf will treat 42 as a char*, will most likely segfault
2+
printf("%s\n", 42); // BAD: printf will treat 42 as a char*, will most likely segfault
33
return 0;
44
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
printf("%i\n", 42); // GOOD: printf will treat 42 as an int
3+
return 0;
4+
}

0 commit comments

Comments
 (0)