Skip to content

Commit 6c095d8

Browse files
authored
Merge pull request github#12953 from MathiasVP/fp-invalid-deref
C++: Add FP for `cpp/invalid-pointer-deref`
2 parents 246d904 + e46c53a commit 6c095d8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/InvalidPointerDeref.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ edges
575575
| test.cpp:213:6:213:6 | q | test.cpp:213:5:213:13 | Store: ... = ... |
576576
| test.cpp:213:6:213:6 | q | test.cpp:213:5:213:13 | Store: ... = ... |
577577
| test.cpp:221:17:221:22 | call to malloc | test.cpp:222:5:222:5 | p |
578+
| test.cpp:231:18:231:30 | new[] | test.cpp:232:3:232:9 | newname |
579+
| test.cpp:232:3:232:9 | newname | test.cpp:232:3:232:16 | access to array |
580+
| test.cpp:232:3:232:16 | access to array | test.cpp:232:3:232:20 | Store: ... = ... |
581+
| test.cpp:238:20:238:32 | new[] | test.cpp:239:5:239:11 | newname |
582+
| test.cpp:239:5:239:11 | newname | test.cpp:239:5:239:18 | access to array |
583+
| test.cpp:239:5:239:18 | access to array | test.cpp:239:5:239:22 | Store: ... = ... |
584+
| test.cpp:248:24:248:30 | call to realloc | test.cpp:249:9:249:9 | p |
585+
| test.cpp:248:24:248:30 | call to realloc | test.cpp:250:22:250:22 | p |
586+
| test.cpp:248:24:248:30 | call to realloc | test.cpp:254:9:254:9 | p |
587+
| test.cpp:254:9:254:9 | p | test.cpp:254:9:254:12 | access to array |
588+
| test.cpp:254:9:254:12 | access to array | test.cpp:254:9:254:16 | Store: ... = ... |
578589
#select
579590
| test.cpp:6:14:6:15 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
580591
| test.cpp:8:14:8:21 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:8:14:8:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
@@ -593,3 +604,6 @@ edges
593604
| test.cpp:171:9:171:14 | Store: ... = ... | test.cpp:143:18:143:23 | call to malloc | test.cpp:171:9:171:14 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:143:18:143:23 | call to malloc | call to malloc | test.cpp:144:29:144:32 | size | size |
594605
| test.cpp:201:5:201:19 | Store: ... = ... | test.cpp:194:23:194:28 | call to malloc | test.cpp:201:5:201:19 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:194:23:194:28 | call to malloc | call to malloc | test.cpp:195:21:195:23 | len | len |
595606
| test.cpp:213:5:213:13 | Store: ... = ... | test.cpp:205:23:205:28 | call to malloc | test.cpp:213:5:213:13 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:205:23:205:28 | call to malloc | call to malloc | test.cpp:206:21:206:23 | len | len |
607+
| test.cpp:232:3:232:20 | Store: ... = ... | test.cpp:231:18:231:30 | new[] | test.cpp:232:3:232:20 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:231:18:231:30 | new[] | new[] | test.cpp:232:11:232:15 | index | index |
608+
| test.cpp:239:5:239:22 | Store: ... = ... | test.cpp:238:20:238:32 | new[] | test.cpp:239:5:239:22 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:238:20:238:32 | new[] | new[] | test.cpp:239:13:239:17 | index | index |
609+
| test.cpp:254:9:254:16 | Store: ... = ... | test.cpp:248:24:248:30 | call to realloc | test.cpp:254:9:254:16 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:248:24:248:30 | call to realloc | call to realloc | test.cpp:254:11:254:11 | i | i |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,35 @@ void test14(unsigned long n, char *p) {
222222
p[n - 1] = 'a'; // GOOD
223223
}
224224
}
225+
226+
void test15(unsigned index) {
227+
unsigned size = index + 13;
228+
if(size < index) {
229+
return;
230+
}
231+
int* newname = new int[size];
232+
newname[index] = 0; // GOOD [FALSE POSITIVE]
233+
}
234+
235+
void test16(unsigned index) {
236+
unsigned size = index + 13;
237+
if(size >= index) {
238+
int* newname = new int[size];
239+
newname[index] = 0; // GOOD [FALSE POSITIVE]
240+
}
241+
}
242+
243+
void *realloc(void *, unsigned);
244+
245+
void test17(unsigned *p, unsigned x, unsigned k) {
246+
if(k > 0 && p[1] <= p[0]){
247+
unsigned n = 3*p[0] + k;
248+
p = (unsigned*)realloc(p, n);
249+
p[0] = n;
250+
unsigned i = p[1];
251+
// The following access is okay because:
252+
// n = 3*p[0] + k >= p[0] + k >= p[1] + k > p[1] = i
253+
// (where p[0] denotes the original value for p[0])
254+
p[i] = x; // GOOD [FALSE POSITIVE]
255+
}
256+
}

0 commit comments

Comments
 (0)