Skip to content

Commit 13fc874

Browse files
committed
C++: Include malloc example in qlhelp
1 parent a43131a commit 13fc874

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
void calls() {
2-
undeclared_function(1, 2);
3-
}
1+
/* '#include <stdlib.h>' was forgotton */
2+
3+
int main(void) {
4+
/* 'int malloc()' assumed */
5+
unsigned char *p = malloc(100);
6+
*p = 'a';
7+
return 0;
8+
}

cpp/ql/src/Likely Bugs/Underspecified Functions/ImplicitFunctionDeclaration.qlhelp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
<p>A function is called without a prior function declaration or definition.
99
When this happens, the compiler generates an implicit declaration of the function,
1010
specifying an integer return type and no parameters.
11-
</p>
11+
If the implicit declaration does not match the true signature of the function, the
12+
function may behave unpredictably.</p>
1213

1314
<p>This may indicate a misspelled function name, or that the required header containing
1415
the function declaration has not been included.</p>
1516

1617
</overview>
1718
<recommendation>
18-
<p>Ensure that the called function has been declared previously in the file or an included header file.</p>
19+
<p>Provide an explicit declaration of the function before invoking it.</p>
1920

2021
</recommendation>
2122
<example><sample src="ImplicitFunctionDeclaration.c" />
@@ -25,4 +26,4 @@ the function declaration has not been included.</p>
2526
<references>
2627
<li>SEI CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/DCL31-C.+Declare+identifiers+before+using+them">DCL31-C. Declare identifiers before using them</a></li>
2728
</references>
28-
</qhelp>
29+
</qhelp>

0 commit comments

Comments
 (0)