Skip to content

Commit 84f9c9b

Browse files
committed
C++: query help for ConstantSizeArrayOffByOne.ql
1 parent 159f11c commit 84f9c9b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#define MAX_SIZE 1024
2+
3+
struct FixedArray {
4+
int buf[MAX_SIZE];
5+
};
6+
7+
int main(){
8+
FixedArray arr;
9+
10+
for(int i = 0; i <= MAX_SIZE; i++) {
11+
arr[i] = 0
12+
}
13+
14+
for(int i = 0; i < MAX_SIZE; i++) {
15+
arr[i = 0]
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>The program performs an out-of-bounds read or write operation. In addition to causing program instability, techniques exist which may allow an attacker to use this vulnerability to execute arbitrary code.</p>
7+
8+
</overview>
9+
<recommendation>
10+
11+
<p>Ensure that pointer dereferences are properly guarded to ensure that they cannot be used to read or write past the end of the allocation.</p>
12+
13+
</recommendation>
14+
<example>
15+
<p>The first example uses a for loop which is improperly bounded by a non-strict less-than operation and will write one position past the end of the array. The second example bounds the for loop properly with a strict less-than operation.</p>
16+
<sample src="ConstantSizeArrayOffByOne.cpp" />
17+
18+
</example>
19+
<references>
20+
21+
<li>CERT C Coding Standard:
22+
<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>
23+
<li>
24+
OWASP:
25+
<a href="https://owasp.org/www-community/vulnerabilities/Buffer_Overflow">Buffer Overflow</a>.
26+
</li>
27+
28+
</references>
29+
</qhelp>

0 commit comments

Comments
 (0)