Skip to content

Commit 01de416

Browse files
authored
Merge pull request #453 from felicity-semmle/cpp/SD-2777-cwe-qhelp-2
C++: Update to bring into line with current guidelines, part 2 (SD-2777)
2 parents df202ef + c6af799 commit 01de416

23 files changed

+151
-122
lines changed

cpp/ql/src/Critical/DescriptorMayNotBeClosed.qhelp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66

77
<overview>
88
<p>
9-
This rule looks at functions that return file or socket descriptors, but may return an error value before actually closing the resource.
10-
This can occur when an operation performed on the open descriptor fails, and the function returns with an error before closing the open resource. An improperly handled error could cause the function to leak resource descriptors.
9+
This query looks at functions that return file or socket descriptors, but may return an error value before actually closing the resource.
10+
This can occur when an operation performed on the open descriptor fails, and the function returns with an error before it closes the open resource. An improperly handled error could cause the function to leak resource descriptors. Failing to close resources in the function that opened them also makes it more difficult to detect leaks.
1111
</p>
1212

1313
<include src="dataFlowWarning.qhelp" />
14-
1514
</overview>
16-
<recommendation>
17-
<p>Ensure that the function frees all the resources it acquired when an error occurs.</p>
1815

16+
<recommendation>
17+
<p>When an error occurs, ensure that the function frees all the resources it holds open.</p>
1918
</recommendation>
19+
2020
<example>
21+
<p>In the example below, the <code>sockfd</code> socket may remain open if an error is triggered.
22+
The code should be updated to ensure that the socket is always closed when when the function ends.
23+
</p>
2124
<sample src="DescriptorMayNotBeClosed.cpp" />
22-
23-
24-
25-
26-
2725
</example>
26+
27+
<references>
28+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR57-CPP.+Do+not+leak+resources+when+handling+exceptions">ERR57-CPP. Do not leak resources when handling exceptions</a>.</li>
29+
</references>
2830
</qhelp>

cpp/ql/src/Critical/DescriptorMayNotBeClosed.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Open descriptor may not be closed
3-
* @description A function may return before closing a socket or file that was opened in the function. Closing resources in the same function that opened them ties the lifetime of the resource to that of the function call, making it easier to avoid and detect resource leaks.
3+
* @description Failing to close resources in the function that opened them makes it difficult to avoid and detect resource leaks.
44
* @kind problem
55
* @id cpp/descriptor-may-not-be-closed
66
* @problem.severity warning

cpp/ql/src/Critical/DescriptorNeverClosed.qhelp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@
66

77
<overview>
88
<p>
9-
This rule finds calls to <code>open</code> or <code>socket</code> with no corresponding <code>close</code> call in the entire program.
9+
This rule finds calls to <code>open</code> or <code>socket</code> where there is no corresponding <code>close</code> call in the program analyzed.
1010
Leaving descriptors open will cause a resource leak that will persist even after the program terminates.
1111
</p>
1212

1313
<include src="aliasAnalysisWarning.qhelp" />
14-
1514
</overview>
15+
1616
<recommendation>
1717
<p>Ensure that all file or socket descriptors allocated by the program are freed before it terminates.</p>
18-
1918
</recommendation>
20-
<example><sample src="DescriptorNeverClosed.cpp" />
21-
2219

20+
<example>
21+
<p>In the example below, the <code>sockfd</code> socket remains open when the <code>main</code> program finishes.
22+
The code should be updated to ensure that the socket is always closed when the program terminates.
23+
</p>
2324

25+
<sample src="DescriptorNeverClosed.cpp" />
2426
</example>
27+
28+
<references>
29+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR57-CPP.+Do+not+leak+resources+when+handling+exceptions">ERR57-CPP. Do not leak resources when handling exceptions</a>.</li>
30+
</references>
2531
</qhelp>

cpp/ql/src/Critical/DescriptorNeverClosed.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Open descriptor never closed
3-
* @description A function always returns before closing a socket or file that was opened in the function. Closing resources in the same function that opened them ties the lifetime of the resource to that of the function call, making it easier to avoid and detect resource leaks.
3+
* @description Functions that always return before closing the socket or file they opened leak resources.
44
* @kind problem
55
* @id cpp/descriptor-never-closed
66
* @problem.severity warning

cpp/ql/src/Critical/InitialisationNotRun.qhelp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55

66

77
<overview>
8-
<p>The rule finds code that initializes a global variable (i.e. uses it as an L-value) but is never called from <code>main</code>.
8+
<p>The query finds code that initializes a global variable (that is, uses it as an L-value) but is never called from <code>main</code>.
99
Unless there is another entry point that triggers the initialization, the code should be modified so that the variable is initialized.
1010
Uninitialized variables may contain any value, as not all compilers generate code that zero-out memory, especially when
1111
optimizations are enabled or the compiler is not compliant with the latest language standards.
1212
</p>
1313

1414
<include src="callGraphWarning.qhelp" />
15-
1615
</overview>
16+
1717
<recommendation>
18-
<p>Examine the code and ensure that the initialization is always run.</p>
18+
<p>Examine the code and ensure that the variable is always initialized before it is used.</p>
1919

2020
</recommendation>
21-
<example><sample src="InitialisationNotRun.cpp" />
22-
23-
24-
21+
<example>
22+
<p>In the example below, the code that triggers the initialization of <code>g_storage</code> is not run from <code>main</code>.
23+
Unless the variable is initialized by another method, the call on line 10 may dereference a null pointer.
24+
</p>
2525

26+
<sample src="InitialisationNotRun.cpp" />
2627
</example>
28+
29+
<references>
30+
<li>C++ reference: <a href="https://en.cppreference.com/book/uninitialized">uninitialized variables</a>.</li>
31+
</references>
2732
</qhelp>

cpp/ql/src/Critical/InitialisationNotRun.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Initialization code not run
3-
* @description A global variable has initialization code, but that code is never run (i.e. is called directly or indirectly from main()). Accessing uninitialized variables leads to undefined results.
3+
* @description Not running initialization code may lead to unexpected behavior.
44
* @kind problem
55
* @id cpp/initialization-not-run
66
* @problem.severity warning

cpp/ql/src/Critical/LateNegativeTest.qhelp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,39 @@
66

77
<overview>
88
<p>
9-
This rule finds integer values that are first used to index an array and
9+
This query finds integer values that are first used to index an array and
1010
subsequently tested for being negative. If it is relevant to perform this test
11-
at all then it should probably happen <em>before</em> the indexing, not
11+
at all then it should happen <em>before</em> the indexing, not
1212
after. Otherwise, if the value is negative then the program will have failed
1313
before performing the test.
1414
</p>
15-
<include src="dataFlowWarning.qhelp" />
1615

16+
<include src="dataFlowWarning.qhelp" />
1717
</overview>
18+
1819
<recommendation>
1920
<p>
20-
See if the value needs checking before being used as array index. Double-check
21+
See if the value needs to be checked before being used as array index. Double-check
2122
if the value is derived from user input. If the value clearly cannot be
2223
negative then the negativity test is redundant and can be removed.
2324
</p>
24-
2525
</recommendation>
26-
<example>
27-
<sample src="LateNegativeTest.cpp" />
28-
29-
30-
3126

27+
<example>
28+
<p>The example below includes two functions that use the value <code>recordIdx</code> to
29+
index an array and a test to verify that the value is positive.
30+
The test is made after <code>records</code> is indexed for <code>printRecord</code> and
31+
before <code>records</code> is indexed for <code>processRecord</code>.
32+
Unless the value of <code>recordIdx</code> cannot be negative, the test should be
33+
updated to run before <em>both</em> times the array is indexed.
34+
If the value cannot be negative, the test should be removed.
35+
</p>
3236

37+
<sample src="LateNegativeTest.cpp" />
3338
</example>
39+
40+
<references>
41+
<li>cplusplus.com: <a href="http://www.cplusplus.com/doc/tutorial/pointers/">Pointers</a>.</li>
42+
<li>SEI CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts">ARR30-C. Do not form or use out-of-bounds pointers or array subscripts</a>.</li>
43+
</references>
3444
</qhelp>

cpp/ql/src/Critical/LateNegativeTest.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @name Pointer offset used before it is checked
3-
* @description A value is used as a pointer offset before it is tested for
4-
* being positive/negative. This may mean that an unsuitable
5-
* pointer offset will be used before the test occurs.
3+
* @description Accessing a pointer or array using an offset before
4+
* checking if the value is positive
5+
* may result in unexpected behavior.
66
* @kind problem
77
* @id cpp/late-negative-test
88
* @problem.severity warning

cpp/ql/src/Critical/MissingNegativityTest.qhelp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,35 @@
66

77
<overview>
88
<p>
9-
This rule finds pointer arithmetic expressions that use a value returned from a function before the value is checked to be positive.
10-
Most pointer arithmetic and almost all array element accesses use a positive value for offsets. A negative value is more likely than not
9+
This query finds pointer arithmetic expressions that use a value returned from a function without checking that the value is positive.
10+
Most pointer arithmetic and almost all array element accesses use a positive value for offsets. A negative value is likely to be
1111
a defect in the returning function. Checking pointer offsets (particularly if they derive from user input) is necessary to avoid
1212
buffer overruns.
1313
</p>
1414

1515
<p>
16-
The rules only looks at return values of functions that may return a negative value (not all functions).
16+
The query looks only at the return values of functions that may return a negative value (not all functions).
1717
</p>
1818

19-
2019
<include src="dataFlowWarning.qhelp" />
21-
2220
</overview>
21+
2322
<recommendation>
2423
<p>
25-
Check the function and see whether it needs to check the value to be positive.
24+
Review the function. Determine whether it needs to check that the value is positive before performing pointer arithmetic.
2625
</p>
27-
2826
</recommendation>
29-
<example>
30-
<sample src="MissingNegativityTest.cpp" />
31-
32-
33-
34-
3527

28+
<example>
29+
<p>The example below shows an example of this problem. There is no check to ensure that the value of <code>recordIdx</code>
30+
is positive and safe to use as an array offset.
31+
</p>
3632

33+
<sample src="MissingNegativityTest.cpp" />
3734
</example>
35+
36+
<references>
37+
<li>cplusplus.com: <a href="http://www.cplusplus.com/doc/tutorial/pointers/">Pointers</a>.</li>
38+
<li>SEI CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts">ARR30-C. Do not form or use out-of-bounds pointers or array subscripts</a>.</li>
39+
</references>
3840
</qhelp>

cpp/ql/src/Critical/MissingNegativityTest.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @name Unchecked return value used as offset
3-
* @description A return value from a function is used as a pointer offset before it is checked for being positive/negative. Integer values used as pointer offsets should be checked, especially if they are derived from user input.
3+
* @description Using a return value as a pointer offset without checking that the value is positive
4+
* may lead to buffer overruns.
45
* @kind problem
56
* @id cpp/missing-negativity-test
67
* @problem.severity warning

0 commit comments

Comments
 (0)