Skip to content

Commit 0d7c5ea

Browse files
authored
Merge pull request #441 from felicity-semmle/cpp/SD-2777-cwe-qhelp
C++: Bring qhelp inline with current guidelines, part 1 (SD-2777)
2 parents 5f118d4 + 1776ebd commit 0d7c5ea

11 files changed

+95
-69
lines changed

cpp/ql/src/Critical/DeadCodeCondition.qhelp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55

66

77
<overview>
8-
<p>This rule finds branching statements with conditions that always evaluate to the same value.
9-
More likely than not these conditions indicate a defect in the branching condition or are an artifact left behind after debugging.</p>
8+
<p>This query finds branching statements with conditions that always evaluate to the same value.
9+
It is likely that these conditions indicate an error in the branching condition.
10+
Alternatively, the conditions may have been left behind after debugging.</p>
1011

1112
<include src="aliasAnalysisWarning.qhelp" />
12-
1313
</overview>
14-
<recommendation>
15-
<p>Check the branch condition for defects, and verify that it isn't a remnant from debugging.</p>
1614

15+
<recommendation>
16+
<p>Check the branch condition for logic errors. Check whether it is still required.</p>
1717
</recommendation>
18-
<example><sample src="DeadCodeCondition.cpp" />
19-
20-
21-
22-
23-
2418

19+
<example>
20+
<p>This example shows two branch conditions that always evaluate to the same value.
21+
The two conditions and their associated branches should be deleted.
22+
This will simplify the code and make it easier to maintain.</p>
2523

24+
<sample src="DeadCodeCondition.cpp" />
2625
</example>
26+
27+
<references>
28+
<li>SEI CERT C++ Coding Standard <a href="https://wiki.sei.cmu.edu/confluence/display/c/MSC12-C.+Detect+and+remove+code+that+has+no+effect+or+is+never+executed">MSC12-C. Detect and remove code that has no effect or is never executed</a>.</li>
29+
</references>
2730
</qhelp>

cpp/ql/src/Critical/DeadCodeFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class C {
22
public:
33
void g() {
44
...
5-
//f() was previously used but is now commented, orphaning f()
5+
//f() was previously used but is now commented-out, orphaning f()
66
//f();
77
...
88
}

cpp/ql/src/Critical/DeadCodeFunction.qhelp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
"qhelp.dtd">
44
<qhelp>
55

6-
76
<overview>
8-
<p>This rule finds functions that are non-public, non-virtual and are never called. Dead functions are often deprecated pieces of code, and should be removed
9-
as they may increase object code size, decrease code comprehensibility, and create the possibility of misuse.</p>
7+
<p>This query highlights functions that are non-public, non-virtual, and are never called.
8+
Dead functions are often deprecated pieces of code, and should be removed.
9+
If left in the code base they increase object code size, decrease code comprehensibility, and create the possibility of misuse.</p>
1010

1111
<p>
12-
<code>public</code> and <code>protected</code> functions are not considered by the check, as they could be part of the program's
13-
API and could be used by external programs.
12+
<code>public</code> and <code>protected</code> functions are ignored by this query.
13+
This type of function may be part of the program's API and could be used by external programs.
1414
</p>
1515

1616
<include src="callGraphWarning.qhelp" />
17-
1817
</overview>
19-
<recommendation>
20-
<p>Consider removing the function.</p>
2118

19+
<recommendation>
20+
<p>Verify that the function is genuinely unused and consider removing it.</p>
2221
</recommendation>
23-
<example><sample src="DeadCodeFunction.cpp" />
24-
2522

2623

24+
<example>
25+
<p>The example below includes a function <code>f</code> that is no longer used and should be deleted.</p>
26+
<sample src="DeadCodeFunction.cpp" />
27+
</example>
2728

29+
<references>
30+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/MSC12-C.+Detect+and+remove+code+that+has+no+effect+or+is+never+executed">MSC12-C. Detect and remove code that has no effect or is never executed</a>.</li>
31+
</references>
2832

29-
</example>
3033
</qhelp>

cpp/ql/src/Critical/DeadCodeFunction.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Function is never called
3-
* @description A function is never called, and should be considered for removal. Unused functions may increase object size, decrease readability and create the possibility of misuse.
3+
* @description Unused functions may increase object size, decrease readability, and create the possibility of misuse.
44
* @kind problem
55
* @id cpp/dead-code-function
66
* @problem.severity warning

cpp/ql/src/Critical/GlobalUseBeforeInit.qhelp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@
55

66

77
<overview>
8-
<p>This rule finds calls to functions that use a global variable which happen before the variable was initialized.
8+
<p>This rule finds calls to functions that use a global variable before the variable has been initialized.
99
Not all compilers generate code that zero-out memory, especially when optimizations are enabled or the compiler
1010
is not compliant with the latest language standards. Accessing uninitialized memory will lead to undefined results.
1111
</p>
1212

1313
<include src="dataFlowWarning.qhelp" />
14-
1514
</overview>
15+
1616
<recommendation>
1717
<p>
1818
Initialize the global variable. If no constant can be used for initialization, ensure that all accesses to the variable occur after
1919
the initialization code is executed.
2020
</p>
21-
2221
</recommendation>
23-
<example><sample src="GlobalUseBeforeInit.cpp" />
24-
25-
26-
27-
28-
22+
<example>
23+
<p>
24+
In the example below, <code>callCtr</code> is wrongly used before it has been initialized.
25+
</p>
26+
<sample src="GlobalUseBeforeInit.cpp" />
2927
</example>
28+
29+
<references>
30+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP53-CPP.+Do+not+read+uninitialized+memory">EXP53-CPP. Do not read uninitialized memory</a>.</li>
31+
</references>
3032
</qhelp>

cpp/ql/src/Critical/GlobalUseBeforeInit.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @name Global variable used before initialization
3-
* @description A function that uses a global variable has been called before the variable has been initialized. Not all compilers zero-out memory for variables, especially when optimizations are enabled, or if the compiler is not compliant with the latest language standards. Using an uninitialized variable leads to undefined results.
2+
* @name Global variable may be used before initialization
3+
* @description Using an uninitialized variable may lead to undefined results.
44
* @kind problem
55
* @id cpp/global-use-before-init
66
* @problem.severity warning

cpp/ql/src/Critical/InconsistentNullnessTesting.qhelp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55

66

77
<overview>
8-
<p>This rule finds pointer dereferences that do not check the pointer for nullness, while the same pointer is checked for nullness in other
9-
places in the code. It is most likely that the nullness check was omitted, and that a NULL pointer dereference can occur.
10-
Dereferencing a null pointer and attempting to modify its contents can lead to anything from a segfault to corrupting
11-
important system data (i.e. the interrupt table in some architectures).
8+
<p>This query finds pointer dereferences that do not first check the pointer for nullness,
9+
even though the same pointer is checked for nullness in other
10+
parts of the code. It is likely that the nullness check was accidentally omitted, and that a null pointer dereference can occur.
11+
Dereferencing a null pointer and attempting to modify its contents can lead to anything from a segmentation fault to corrupting
12+
important system data (including the interrupt table in some architectures).
1213
</p>
1314

1415
<include src="pointsToWarning.qhelp" />
15-
1616
</overview>
17+
1718
<recommendation>
18-
<p>Make the nullness check on the pointer consistent across all dereferences.</p>
19+
<p>Use a nullness check consistently in all cases where a pointer is dereferenced.</p>
1920

2021
</recommendation>
21-
<example><sample src="InconsistentNullnessTesting.cpp" />
22-
23-
24-
25-
22+
<example>
23+
<p>
24+
This code shows two examples where a pointer is dereferenced.
25+
The first example checks that the pointer is not null before dereferencing it.
26+
The second example fails to perform a nullness check, leading to a potential vulnerability in the code.
27+
</p>
28+
<sample src="InconsistentNullnessTesting.cpp" />
2629
</example>
30+
31+
<references>
32+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/MEM10-C.+Define+and+use+a+pointer+validation+function">MEM10-C. Define and use a pointer validation function</a>.</li>
33+
</references>
2734
</qhelp>

cpp/ql/src/Critical/InconsistentNullnessTesting.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Inconsistent null check of pointer
3-
* @description A dereferenced pointer is not checked for nullness in the given location, but is checked in other locations. Dereferencing a NULL pointer leads to undefined results.
3+
* @description A dereferenced pointer is not checked for nullness in this location, but it is checked in other locations. Dereferencing a null pointer leads to undefined results.
44
* @kind problem
55
* @id cpp/inconsistent-nullness-testing
66
* @problem.severity warning

cpp/ql/src/Critical/OverflowCalculated.qhelp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,43 @@
66

77
<overview>
88
<p>
9-
This rule finds <code>malloc</code> that use a <code>strlen</code> for the size but to not take the
10-
zero terminator into consideration, and <code>strcat/strncat</code> calls that are done on buffers that do
11-
not have the sufficient size to contain the new string.
12-
</p>
9+
This query finds calls to:</p>
10+
<ul>
11+
<li><code>malloc</code> that use a <code>strlen</code> for the buffer size and do not take the
12+
zero terminator into consideration.</li>
13+
<li><code>strcat</code> or <code>strncat</code> that use buffers that are too small to contain the new string.</li>
14+
</ul>
1315

1416
<p>
15-
The indicated expression will cause a buffer overflow due to a buffer that is of insufficient size to contain
16-
the data being copied. Buffer overflows can result to anything from a segfault to a security vulnerability (particularly
17+
The highlighted expression will cause a buffer overflow because the buffer is too small to contain
18+
the data being copied. Buffer overflows can result to anything from a segmentation fault to a security vulnerability (particularly
1719
if the array is on stack-allocated memory).
1820
</p>
1921

2022
<include src="aliasAnalysisWarning.qhelp" />
21-
2223
</overview>
24+
2325
<recommendation>
2426
<p>
2527
Increase the size of the buffer being allocated.
2628
</p>
27-
2829
</recommendation>
29-
<example><sample src="OverflowCalculated.cpp" />
30-
3130

31+
<example>
32+
<p>This example includes three annotated calls that copy a string into a buffer.
33+
The first call to <code>malloc</code> creates a buffer that's the
34+
same size as the string, leaving no space for the zero terminator
35+
and causing an overflow. The second call to <code>malloc</code>
36+
correctly calculates the required buffer size. The call to
37+
<code>strcat</code> appends an additional string to the same buffer
38+
causing a second overflow.</p>
3239

40+
<sample src="OverflowCalculated.cpp" />
3341
</example>
34-
<references>
3542

43+
<references>
3644
<li><a href="http://cwe.mitre.org/data/definitions/131.html">CWE-131: Incorrect Calculation of Buffer Size</a></li>
3745
<li>I. Gerg. <em>An Overview and Example of the Buffer-Overflow Exploit</em>. IANewsletter vol 7 no 4. 2005.</li>
3846
<li>M. Donaldson. <em>Inside the Buffer Overflow Attack: Mechanism, Method &amp; Prevention</em>. SANS Institute InfoSec Reading Room. 2002.</li>
39-
4047
</references>
4148
</qhelp>

cpp/ql/src/Critical/OverflowDestination.qhelp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>The bounded copy functions <code>memcpy</code>, <code>memmove</code>, <code>strncpy</code>, <code>strncat</code> accept a size argument. You should call these functions with a size argument that is derived from the size of the destination buffer. Using a size argument that is derived from the source buffer may cause a buffer overflow. Buffer overflows can lead to anything from a segmentation fault to a security vulnerability.</p>
7-
8-
6+
<p>The bounded copy functions <code>memcpy</code>, <code>memmove</code>, <code>strncpy</code>, <code>strncat</code> accept a size argument.
7+
You should call these functions with a size argument that is derived from the size of the destination buffer.
8+
Using a size argument that is derived from the source buffer may cause a buffer overflow.
9+
Buffer overflows can lead to anything from a segmentation fault to a security vulnerability.</p>
910
</overview>
11+
1012
<recommendation>
11-
<p>Check the highlighted function calls carefully, and ensure that the size parameter is derived from the size of the destination buffer,
13+
<p>Check the highlighted function calls carefully.
14+
Ensure that the size parameter is derived from the size of the destination buffer, and
1215
not the source buffer.</p>
1316

1417
<include src="aliasAnalysisWarning.qhelp" />
15-
1618
</recommendation>
17-
<example><sample src="OverflowDestination.cpp" />
1819

1920

21+
<example>
22+
<p>
23+
The code below shows an example where <code>strncpy</code> is called incorrectly, without checking the size of the destination buffer.
24+
In the second example the call has been updated to include the size of the destination buffer.</p>
2025

26+
<sample src="OverflowDestination.cpp" />
2127
</example>
22-
<references>
2328

24-
<li><a href="http://cwe.mitre.org/data/definitions/119.html">CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer</a></li>
29+
<references>
2530
<li>I. Gerg. <em>An Overview and Example of the Buffer-Overflow Exploit</em>. IANewsletter vol 7 no 4. 2005.</li>
2631
<li>M. Donaldson. <em>Inside the Buffer Overflow Attack: Mechanism, Method &amp; Prevention</em>. SANS Institute InfoSec Reading Room. 2002.</li>
27-
2832
</references>
2933
</qhelp>

0 commit comments

Comments
 (0)