Skip to content

Commit 26b0363

Browse files
committed
CPP: Add test demonstrating use-after-free false negatives.
1 parent 07eb60d commit 26b0363

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

cpp/ql/test/query-tests/Critical/MemoryFreed/MemoryFreed.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@
9797
| test_free.cpp:260:9:260:9 | p |
9898
| test_free.cpp:263:12:263:12 | p |
9999
| test_free.cpp:269:7:269:11 | ... = ... |
100+
| test_free.cpp:277:11:277:13 | buf |
101+
| test_free.cpp:282:10:282:12 | buf |
102+
| test_free.cpp:288:8:288:10 | buf |
103+
| test_free.cpp:293:8:293:10 | buf |
100104
| virtual.cpp:18:10:18:10 | a |
101105
| virtual.cpp:19:10:19:10 | c |
102106
| virtual.cpp:38:10:38:10 | b |

cpp/ql/test/query-tests/Critical/MemoryFreed/UseAfterFree.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ edges
1212
| test_free.cpp:233:14:233:15 | * ... | test_free.cpp:236:9:236:10 | * ... |
1313
| test_free.cpp:239:14:239:15 | * ... | test_free.cpp:241:9:241:10 | * ... |
1414
| test_free.cpp:245:10:245:11 | * ... | test_free.cpp:246:9:246:10 | * ... |
15+
| test_free.cpp:293:8:293:10 | buf | test_free.cpp:294:3:294:13 | ... = ... |
16+
| test_free.cpp:294:3:294:13 | ... = ... | test_free.cpp:294:5:294:7 | s indirection [post update] [buf] |
17+
| test_free.cpp:294:5:294:7 | s indirection [post update] [buf] | test_free.cpp:295:12:295:12 | s indirection [buf] |
18+
| test_free.cpp:295:12:295:12 | s indirection [buf] | test_free.cpp:295:14:295:16 | buf |
1519
nodes
1620
| test_free.cpp:11:10:11:10 | a | semmle.label | a |
1721
| test_free.cpp:12:5:12:5 | a | semmle.label | a |
@@ -38,6 +42,11 @@ nodes
3842
| test_free.cpp:241:9:241:10 | * ... | semmle.label | * ... |
3943
| test_free.cpp:245:10:245:11 | * ... | semmle.label | * ... |
4044
| test_free.cpp:246:9:246:10 | * ... | semmle.label | * ... |
45+
| test_free.cpp:293:8:293:10 | buf | semmle.label | buf |
46+
| test_free.cpp:294:3:294:13 | ... = ... | semmle.label | ... = ... |
47+
| test_free.cpp:294:5:294:7 | s indirection [post update] [buf] | semmle.label | s indirection [post update] [buf] |
48+
| test_free.cpp:295:12:295:12 | s indirection [buf] | semmle.label | s indirection [buf] |
49+
| test_free.cpp:295:14:295:16 | buf | semmle.label | buf |
4150
subpaths
4251
#select
4352
| test_free.cpp:12:5:12:5 | a | test_free.cpp:11:10:11:10 | a | test_free.cpp:12:5:12:5 | a | Memory may have been previously freed by $@. | test_free.cpp:11:5:11:8 | call to free | call to free |
@@ -53,3 +62,4 @@ subpaths
5362
| test_free.cpp:236:9:236:10 | * ... | test_free.cpp:233:14:233:15 | * ... | test_free.cpp:236:9:236:10 | * ... | Memory may have been previously freed by $@. | test_free.cpp:233:9:233:12 | call to free | call to free |
5463
| test_free.cpp:241:9:241:10 | * ... | test_free.cpp:239:14:239:15 | * ... | test_free.cpp:241:9:241:10 | * ... | Memory may have been previously freed by $@. | test_free.cpp:239:9:239:12 | call to free | call to free |
5564
| test_free.cpp:246:9:246:10 | * ... | test_free.cpp:245:10:245:11 | * ... | test_free.cpp:246:9:246:10 | * ... | Memory may have been previously freed by $@. | test_free.cpp:245:5:245:8 | call to free | call to free |
65+
| test_free.cpp:295:14:295:16 | buf | test_free.cpp:293:8:293:10 | buf | test_free.cpp:295:14:295:16 | buf | Memory may have been previously freed by $@. | test_free.cpp:293:3:293:6 | call to free | call to free |

cpp/ql/test/query-tests/Critical/MemoryFreed/test_free.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,30 @@ void test_free_assign() {
267267
void *a = malloc(10);
268268
void *b;
269269
free(b = a); // GOOD
270+
}
271+
272+
struct MyStruct {
273+
char* buf;
274+
};
275+
276+
void test_free_struct(MyStruct* s) {
277+
free(s->buf);
278+
char c = s->buf[0]; // BAD [FALSE NEGATIVE]
279+
}
280+
281+
void test_free_struct2(MyStruct s) {
282+
free(s.buf);
283+
char c = s.buf[0]; // BAD [FALSE NEGATIVE]
284+
}
285+
286+
void test_free_struct3(MyStruct s) {
287+
char* buf = s.buf;
288+
free(buf);
289+
char c = s.buf[0]; // BAD [FALSE NEGATIVE]
290+
}
291+
292+
void test_free_struct4(char* buf, MyStruct s) {
293+
free(buf);
294+
s.buf = buf;
295+
char c = s.buf[0]; // BAD
270296
}

0 commit comments

Comments
 (0)