Skip to content

Commit 99d7512

Browse files
committed
C++: tests for constant-size off-by-one query
1 parent 447c11c commit 99d7512

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| test.cpp:35:5:35:22 | PointerAdd: access to array | This pointer may have an off-by-1 error allowing it to overrun $@ | test.cpp:15:9:15:11 | buf | buf |
2+
| test.cpp:36:5:36:24 | PointerAdd: access to array | This pointer may have an off-by-2 error allowing it to overrun $@ | test.cpp:15:9:15:11 | buf | buf |
3+
| test.cpp:43:9:43:19 | PointerAdd: access to array | This pointer may have an off-by-1 error allowing it to overrun $@ | test.cpp:15:9:15:11 | buf | buf |
4+
| test.cpp:49:5:49:22 | PointerAdd: access to array | This pointer may have an off-by-1 error allowing it to overrun $@ | test.cpp:19:9:19:11 | buf | buf |
5+
| test.cpp:50:5:50:24 | PointerAdd: access to array | This pointer may have an off-by-2 error allowing it to overrun $@ | test.cpp:19:9:19:11 | buf | buf |
6+
| test.cpp:57:9:57:19 | PointerAdd: access to array | This pointer may have an off-by-1 error allowing it to overrun $@ | test.cpp:19:9:19:11 | buf | buf |
7+
| test.cpp:61:9:61:19 | PointerAdd: access to array | This pointer may have an off-by-2 error allowing it to overrun $@ | test.cpp:19:9:19:11 | buf | buf |
8+
| test.cpp:77:27:77:44 | PointerAdd: access to array | This pointer may have an off-by-1 error allowing it to overrun $@ | test.cpp:15:9:15:11 | buf | buf |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#define MAX_SIZE 1024
2+
3+
struct ZeroArray {
4+
int size;
5+
int buf[0];
6+
};
7+
8+
struct OneArray {
9+
int size;
10+
int buf[1];
11+
};
12+
13+
struct BigArray {
14+
int size;
15+
int buf[MAX_SIZE];
16+
};
17+
18+
struct ArrayAndFields {
19+
int buf[MAX_SIZE];
20+
int field1;
21+
int field2;
22+
};
23+
24+
// tests for dynamic-size trailing arrays
25+
void testZeroArray(ZeroArray *arr) {
26+
arr->buf[0] = 0;
27+
}
28+
29+
void testOneArray(OneArray *arr) {
30+
arr->buf[1] = 0;
31+
}
32+
33+
void testBig(BigArray *arr) {
34+
arr->buf[MAX_SIZE-1] = 0; // GOOD
35+
arr->buf[MAX_SIZE] = 0; // BAD
36+
arr->buf[MAX_SIZE+1] = 0; // BAD
37+
38+
for(int i = 0; i < MAX_SIZE; i++) {
39+
arr->buf[i] = 0; // GOOD
40+
}
41+
42+
for(int i = 0; i <= MAX_SIZE; i++) {
43+
arr->buf[i] = 0; // BAD
44+
}
45+
}
46+
47+
void testFields(ArrayAndFields *arr) {
48+
arr->buf[MAX_SIZE-1] = 0; // GOOD
49+
arr->buf[MAX_SIZE] = 0; // BAD?
50+
arr->buf[MAX_SIZE+1] = 0; // BAD?
51+
52+
for(int i = 0; i < MAX_SIZE; i++) {
53+
arr->buf[i] = 0; // GOOD
54+
}
55+
56+
for(int i = 0; i <= MAX_SIZE; i++) {
57+
arr->buf[i] = 0; // BAD?
58+
}
59+
60+
for(int i = 0; i < MAX_SIZE+2; i++) {
61+
arr->buf[i] = 0; // BAD?
62+
}
63+
// is this different if it's a memcpy?
64+
}
65+
66+
void assignThroughPointer(int *p) {
67+
*p = 0; // ??? should the result go at a flow source?
68+
}
69+
70+
void addToPointerAndAssign(int *p) {
71+
p[MAX_SIZE-1] = 0; // GOOD
72+
p[MAX_SIZE] = 0; // BAD
73+
}
74+
75+
void testInterproc(BigArray *arr) {
76+
assignThroughPointer(&arr->buf[MAX_SIZE-1]); // GOOD
77+
assignThroughPointer(&arr->buf[MAX_SIZE]); // BAD
78+
79+
addToPointerAndAssign(arr->buf);
80+
}

0 commit comments

Comments
 (0)