Skip to content

Commit 880d56c

Browse files
authored
Merge pull request #16404 from geoffw0/qhelp2
C++: Improve qhelp for WrongTypeFormatArguments.
2 parents 9c8945f + 657402b commit 880d56c

6 files changed

+26
-23
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ function.
2222
</example>
2323
<references>
2424

25-
<li>cplusplus.com: <a href="http://www.tutorialspoint.com/cplusplus/cpp_functions.htm">C++ Functions</a>.</li>
25+
<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>
2626
<li>Microsoft C Runtime Library Reference: <a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l">printf, wprintf</a>.</li>
2727

28-
29-
3028
</references>
3129
</qhelp>

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ contents.
1919

2020
</overview>
2121
<recommendation>
22-
<p>Review the format and arguments expected by the highlighted function calls. Update either
23-
the format or the arguments so that the expected number of arguments are passed to the
22+
<p>Review the format and arguments expected by the highlighted function calls. Update either
23+
the format or the arguments so that the expected number of arguments are passed to the
2424
function.
2525
</p>
2626

@@ -30,11 +30,8 @@ function.
3030
</example>
3131
<references>
3232

33-
<li>CERT C Coding
34-
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>
35-
<li>cplusplus.com: <a href="http://www.tutorialspoint.com/cplusplus/cpp_functions.htm">C++ Functions</a>.</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>
3634
<li>Microsoft C Runtime Library Reference: <a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l">printf, wprintf</a>.</li>
3735

38-
3936
</references>
4037
</qhelp>

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

Lines changed: 0 additions & 4 deletions
This file was deleted.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@
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>%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 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>
3034

3135
</references>
3236
</qhelp>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
printf("%s\n", 42); // BAD: printf will treat 42 as a char*, will most likely segfault
3+
return 0;
4+
}
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)