Skip to content

Commit e428502

Browse files
authored
Merge pull request github#13598 from MathiasVP/add-constant-size-array-fp
C++: Add `cpp/constant-array-overflow` FPs
2 parents f9afea8 + ae09882 commit e428502

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/ConstantSizeArrayOffByOne.expected

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ edges
3939
| test.cpp:148:23:148:28 | buffer | test.cpp:151:5:151:11 | access to array |
4040
| test.cpp:159:25:159:29 | array | test.cpp:161:5:161:10 | access to array |
4141
| test.cpp:159:25:159:29 | array | test.cpp:162:5:162:10 | access to array |
42+
| test.cpp:175:30:175:30 | p | test.cpp:191:27:191:30 | access to array |
43+
| test.cpp:198:14:198:20 | buffer1 | test.cpp:175:30:175:30 | p |
44+
| test.cpp:198:14:198:20 | buffer1 | test.cpp:198:14:198:20 | buffer1 |
45+
| test.cpp:201:14:201:20 | buffer2 | test.cpp:175:30:175:30 | p |
46+
| test.cpp:201:14:201:20 | buffer2 | test.cpp:201:14:201:20 | buffer2 |
47+
| test.cpp:204:14:204:20 | buffer3 | test.cpp:175:30:175:30 | p |
48+
| test.cpp:204:14:204:20 | buffer3 | test.cpp:204:14:204:20 | buffer3 |
49+
| test.cpp:207:35:207:35 | p | test.cpp:208:14:208:14 | p |
50+
| test.cpp:208:14:208:14 | p | test.cpp:175:30:175:30 | p |
51+
| test.cpp:213:19:213:25 | buffer1 | test.cpp:207:35:207:35 | p |
52+
| test.cpp:213:19:213:25 | buffer1 | test.cpp:213:19:213:25 | buffer1 |
53+
| test.cpp:216:19:216:25 | buffer2 | test.cpp:207:35:207:35 | p |
54+
| test.cpp:216:19:216:25 | buffer2 | test.cpp:216:19:216:25 | buffer2 |
55+
| test.cpp:219:19:219:25 | buffer3 | test.cpp:207:35:207:35 | p |
56+
| test.cpp:219:19:219:25 | buffer3 | test.cpp:219:19:219:25 | buffer3 |
4257
nodes
4358
| test.cpp:34:5:34:24 | access to array | semmle.label | access to array |
4459
| test.cpp:34:10:34:12 | buf | semmle.label | buf |
@@ -97,6 +112,22 @@ nodes
97112
| test.cpp:159:25:159:29 | array | semmle.label | array |
98113
| test.cpp:161:5:161:10 | access to array | semmle.label | access to array |
99114
| test.cpp:162:5:162:10 | access to array | semmle.label | access to array |
115+
| test.cpp:175:30:175:30 | p | semmle.label | p |
116+
| test.cpp:191:27:191:30 | access to array | semmle.label | access to array |
117+
| test.cpp:198:14:198:20 | buffer1 | semmle.label | buffer1 |
118+
| test.cpp:198:14:198:20 | buffer1 | semmle.label | buffer1 |
119+
| test.cpp:201:14:201:20 | buffer2 | semmle.label | buffer2 |
120+
| test.cpp:201:14:201:20 | buffer2 | semmle.label | buffer2 |
121+
| test.cpp:204:14:204:20 | buffer3 | semmle.label | buffer3 |
122+
| test.cpp:204:14:204:20 | buffer3 | semmle.label | buffer3 |
123+
| test.cpp:207:35:207:35 | p | semmle.label | p |
124+
| test.cpp:208:14:208:14 | p | semmle.label | p |
125+
| test.cpp:213:19:213:25 | buffer1 | semmle.label | buffer1 |
126+
| test.cpp:213:19:213:25 | buffer1 | semmle.label | buffer1 |
127+
| test.cpp:216:19:216:25 | buffer2 | semmle.label | buffer2 |
128+
| test.cpp:216:19:216:25 | buffer2 | semmle.label | buffer2 |
129+
| test.cpp:219:19:219:25 | buffer3 | semmle.label | buffer3 |
130+
| test.cpp:219:19:219:25 | buffer3 | semmle.label | buffer3 |
100131
subpaths
101132
#select
102133
| test.cpp:35:5:35:22 | PointerAdd: access to array | test.cpp:35:10:35:12 | buf | test.cpp:35:5:35:22 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:35:5:35:26 | Store: ... = ... | write |
@@ -113,3 +144,5 @@ subpaths
113144
| test.cpp:136:9:136:16 | PointerAdd: ... += ... | test.cpp:143:18:143:21 | asdf | test.cpp:138:13:138:15 | arr | This pointer arithmetic may have an off-by-2 error allowing it to overrun $@ at this $@. | test.cpp:142:10:142:13 | asdf | asdf | test.cpp:138:12:138:15 | Load: * ... | read |
114145
| test.cpp:151:5:151:11 | PointerAdd: access to array | test.cpp:148:23:148:28 | buffer | test.cpp:151:5:151:11 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:147:19:147:24 | buffer | buffer | test.cpp:151:5:151:15 | Store: ... = ... | write |
115146
| test.cpp:162:5:162:10 | PointerAdd: access to array | test.cpp:159:25:159:29 | array | test.cpp:162:5:162:10 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:158:10:158:14 | array | array | test.cpp:162:5:162:19 | Store: ... = ... | write |
147+
| test.cpp:191:27:191:30 | PointerAdd: access to array | test.cpp:201:14:201:20 | buffer2 | test.cpp:191:27:191:30 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:200:19:200:25 | buffer2 | buffer2 | test.cpp:191:27:191:30 | Load: access to array | read |
148+
| test.cpp:191:27:191:30 | PointerAdd: access to array | test.cpp:216:19:216:25 | buffer2 | test.cpp:191:27:191:30 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:215:19:215:25 | buffer2 | buffer2 | test.cpp:191:27:191:30 | Load: access to array | read |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,52 @@ void pointer_size_larger_than_array_element_size_and_does_not_divide_it() {
169169
ptr[0] = vec3{}; // GOOD: writes ints 0, 1, 2
170170
ptr[1] = vec3{}; // BAD: writes ints 3, 4, 5 [NOT DETECTED]
171171
}
172+
173+
void use(...);
174+
175+
void call_use(unsigned char* p, int n) {
176+
if(n == 0) {
177+
return;
178+
}
179+
if(n == 1) {
180+
unsigned char x = p[0];
181+
use(x);
182+
}
183+
if(n == 2) {
184+
unsigned char x = p[0];
185+
unsigned char y = p[1];
186+
use(x, y);
187+
}
188+
if(n == 3) {
189+
unsigned char x = p[0];
190+
unsigned char y = p[1];
191+
unsigned char z = p[2]; // GOOD [FALSE POSITIVE]: `call_use(buffer2, 2)` won't reach this point.
192+
use(x, y, z);
193+
}
194+
}
195+
196+
void test_call_use() {
197+
unsigned char buffer1[1];
198+
call_use(buffer1,1);
199+
200+
unsigned char buffer2[2];
201+
call_use(buffer2,2);
202+
203+
unsigned char buffer3[3];
204+
call_use(buffer3,3);
205+
}
206+
207+
void call_call_use(unsigned char* p, int n) {
208+
call_use(p, n);
209+
}
210+
211+
void test_call_use2() {
212+
unsigned char buffer1[1];
213+
call_call_use(buffer1,1);
214+
215+
unsigned char buffer2[2];
216+
call_call_use(buffer2,2);
217+
218+
unsigned char buffer3[3];
219+
call_call_use(buffer3,3);
220+
}

0 commit comments

Comments
 (0)