Skip to content

Commit 6d06234

Browse files
committed
C++: Add testcase demonstrating missing result for 'cpp/invalid-pointer-deref' query.
1 parent 82c3e53 commit 6d06234

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,46 @@ edges
609609
| test.cpp:180:19:180:28 | call to mk_array_p indirection [begin] | test.cpp:165:29:165:31 | arr indirection [begin] |
610610
| test.cpp:180:19:180:28 | call to mk_array_p indirection [end] | test.cpp:165:29:165:31 | arr indirection [end] |
611611
| test.cpp:188:15:188:20 | call to malloc | test.cpp:189:15:189:15 | Load |
612+
| test.cpp:194:23:194:28 | call to malloc | test.cpp:195:17:195:17 | Load |
613+
| test.cpp:194:23:194:28 | call to malloc | test.cpp:197:8:197:8 | Load |
614+
| test.cpp:194:23:194:28 | call to malloc | test.cpp:201:5:201:5 | Load |
615+
| test.cpp:205:23:205:28 | call to malloc | test.cpp:206:17:206:17 | Load |
616+
| test.cpp:205:23:205:28 | call to malloc | test.cpp:208:15:208:15 | Load |
617+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | ... + ... |
618+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | ... + ... |
619+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | ... + ... |
620+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | ... + ... |
621+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | Store |
622+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | Store |
623+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | Store |
624+
| test.cpp:206:17:206:17 | Load | test.cpp:206:17:206:23 | Store |
625+
| test.cpp:206:17:206:17 | Load | test.cpp:209:12:209:14 | Load |
626+
| test.cpp:206:17:206:17 | Load | test.cpp:209:12:209:14 | Load |
627+
| test.cpp:206:17:206:17 | Load | test.cpp:209:12:209:14 | Load |
628+
| test.cpp:206:17:206:17 | Load | test.cpp:209:12:209:14 | Load |
629+
| test.cpp:206:17:206:17 | Load | test.cpp:213:5:213:6 | * ... |
630+
| test.cpp:206:17:206:17 | Load | test.cpp:213:5:213:6 | * ... |
631+
| test.cpp:206:17:206:17 | Load | test.cpp:213:5:213:6 | * ... |
632+
| test.cpp:206:17:206:17 | Load | test.cpp:213:5:213:6 | * ... |
633+
| test.cpp:206:17:206:17 | Load | test.cpp:213:6:213:6 | Load |
634+
| test.cpp:206:17:206:17 | Load | test.cpp:213:6:213:6 | Load |
635+
| test.cpp:206:17:206:17 | Load | test.cpp:213:6:213:6 | Load |
636+
| test.cpp:206:17:206:17 | Load | test.cpp:213:6:213:6 | Load |
637+
| test.cpp:206:17:206:23 | ... + ... | test.cpp:206:17:206:23 | Store |
638+
| test.cpp:206:17:206:23 | ... + ... | test.cpp:206:17:206:23 | Store |
639+
| test.cpp:206:17:206:23 | ... + ... | test.cpp:209:12:209:14 | Load |
640+
| test.cpp:206:17:206:23 | ... + ... | test.cpp:213:5:213:13 | Store: ... = ... |
641+
| test.cpp:206:17:206:23 | ... + ... | test.cpp:213:5:213:13 | Store: ... = ... |
642+
| test.cpp:206:17:206:23 | Store | test.cpp:209:12:209:14 | Load |
643+
| test.cpp:206:17:206:23 | Store | test.cpp:213:5:213:13 | Store: ... = ... |
644+
| test.cpp:206:17:206:23 | Store | test.cpp:213:5:213:13 | Store: ... = ... |
645+
| test.cpp:209:12:209:14 | Load | test.cpp:213:5:213:13 | Store: ... = ... |
646+
| test.cpp:209:12:209:14 | Load | test.cpp:213:5:213:13 | Store: ... = ... |
647+
| test.cpp:213:5:213:6 | * ... | test.cpp:213:5:213:13 | Store: ... = ... |
648+
| test.cpp:213:5:213:6 | * ... | test.cpp:213:5:213:13 | Store: ... = ... |
649+
| test.cpp:213:6:213:6 | Load | test.cpp:213:5:213:6 | * ... |
650+
| test.cpp:213:6:213:6 | Load | test.cpp:213:5:213:13 | Store: ... = ... |
651+
| test.cpp:213:6:213:6 | Load | test.cpp:213:5:213:13 | Store: ... = ... |
612652
#select
613653
| 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 |
614654
| 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 |
@@ -625,3 +665,4 @@ edges
625665
| test.cpp:110:9:110:14 | Store: ... = ... | test.cpp:82:17:82:22 | call to malloc | test.cpp:110:9:110:14 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:82:17:82:22 | call to malloc | call to malloc | test.cpp:83:27:83:30 | size | size |
626666
| test.cpp:157:9:157:14 | Store: ... = ... | test.cpp:143:18:143:23 | call to malloc | test.cpp:157:9:157: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 |
627667
| 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 |
668+
| 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 |

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,27 @@ void test11(unsigned size) {
188188
char *p = malloc(size);
189189
char *q = p + size - 1;
190190
deref_plus_one(q);
191-
}
191+
}
192+
193+
void test12(unsigned len, unsigned index) {
194+
char* p = (char *)malloc(len);
195+
char* end = p + len;
196+
197+
if(p + index > end) {
198+
return;
199+
}
200+
201+
p[index] = '\0'; // BAD [NOT DETECTED]
202+
}
203+
204+
void test13(unsigned len, unsigned index) {
205+
char* p = (char *)malloc(len);
206+
char* end = p + len;
207+
208+
char* q = p + index;
209+
if(q > end) {
210+
return;
211+
}
212+
213+
*q = '\0'; // BAD
214+
}

0 commit comments

Comments
 (0)