Skip to content

Commit 1456012

Browse files
committed
C++: Additional test cases for cpp/use-after-free.
1 parent 4920039 commit 1456012

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
| test.cpp:199:10:199:12 | cpy |
2929
| test.cpp:213:7:213:7 | a |
3030
| test.cpp:219:7:219:7 | a |
31+
| test.cpp:228:14:228:18 | data1 |
32+
| test.cpp:236:14:236:18 | data1 |
33+
| test.cpp:237:14:237:18 | data2 |
3134
| test_free.cpp:11:10:11:10 | a |
3235
| test_free.cpp:14:10:14:10 | a |
3336
| test_free.cpp:16:10:16:10 | a |

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
edges
22
| test.cpp:213:7:213:7 | pointer to free output argument | test.cpp:214:2:214:2 | a | provenance | |
33
| test.cpp:219:7:219:7 | pointer to free output argument | test.cpp:220:2:220:2 | a | provenance | |
4+
| test.cpp:228:12:228:12 | *p [post update] [data1] | test.cpp:229:2:229:2 | *p [data1] | provenance | |
5+
| test.cpp:228:14:228:18 | pointer to operator delete[] output argument | test.cpp:228:12:228:12 | *p [post update] [data1] | provenance | |
6+
| test.cpp:229:2:229:2 | *p [data1] | test.cpp:229:4:229:8 | data1 | provenance | |
47
| test_free.cpp:11:10:11:10 | pointer to free output argument | test_free.cpp:12:5:12:5 | a | provenance | |
58
| test_free.cpp:11:10:11:10 | pointer to free output argument | test_free.cpp:13:5:13:6 | * ... | provenance | |
69
| test_free.cpp:42:27:42:27 | pointer to free output argument | test_free.cpp:45:5:45:5 | a | provenance | |
@@ -37,6 +40,10 @@ nodes
3740
| test.cpp:214:2:214:2 | a | semmle.label | a |
3841
| test.cpp:219:7:219:7 | pointer to free output argument | semmle.label | pointer to free output argument |
3942
| test.cpp:220:2:220:2 | a | semmle.label | a |
43+
| test.cpp:228:12:228:12 | *p [post update] [data1] | semmle.label | *p [post update] [data1] |
44+
| test.cpp:228:14:228:18 | pointer to operator delete[] output argument | semmle.label | pointer to operator delete[] output argument |
45+
| test.cpp:229:2:229:2 | *p [data1] | semmle.label | *p [data1] |
46+
| test.cpp:229:4:229:8 | data1 | semmle.label | data1 |
4047
| test_free.cpp:11:10:11:10 | pointer to free output argument | semmle.label | pointer to free output argument |
4148
| test_free.cpp:12:5:12:5 | a | semmle.label | a |
4249
| test_free.cpp:13:5:13:6 | * ... | semmle.label | * ... |
@@ -90,6 +97,7 @@ subpaths
9097
#select
9198
| test.cpp:214:2:214:2 | a | test.cpp:213:7:213:7 | pointer to free output argument | test.cpp:214:2:214:2 | a | Memory may have been previously freed by $@. | test.cpp:213:2:213:5 | call to free | call to free |
9299
| test.cpp:220:2:220:2 | a | test.cpp:219:7:219:7 | pointer to free output argument | test.cpp:220:2:220:2 | a | Memory may have been previously freed by $@. | test.cpp:219:2:219:5 | call to free | call to free |
100+
| test.cpp:229:4:229:8 | data1 | test.cpp:228:14:228:18 | pointer to operator delete[] output argument | test.cpp:229:4:229:8 | data1 | Memory may have been previously freed by $@. | test.cpp:228:2:228:18 | delete[] | delete[] |
93101
| test_free.cpp:12:5:12:5 | a | test_free.cpp:11:10:11:10 | pointer to free output argument | 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 |
94102
| test_free.cpp:13:5:13:6 | * ... | test_free.cpp:11:10:11:10 | pointer to free output argument | test_free.cpp:13:5:13:6 | * ... | Memory may have been previously freed by $@. | test_free.cpp:11:5:11:8 | call to free | call to free |
95103
| test_free.cpp:45:5:45:5 | a | test_free.cpp:42:27:42:27 | pointer to free output argument | test_free.cpp:45:5:45:5 | a | Memory may have been previously freed by $@. | test_free.cpp:42:22:42:25 | call to free | call to free |

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ void test_strndupa_dealloc() {
201201

202202
// ---
203203

204-
205-
206-
207-
204+
struct DataPair {
205+
char *data1;
206+
char *data2;
207+
};
208208

209209
void test_reassignment() {
210210
char *a = (char *)malloc(128);
@@ -218,4 +218,21 @@ void test_reassignment() {
218218

219219
free(a);
220220
a[0] = 0; // BAD
221+
222+
DataPair p;
223+
p.data1 = new char[128];
224+
p.data2 = new char[128];
225+
p.data1[0] = 0; // GOOD
226+
p.data2[0] = 0; // GOOD
227+
228+
delete [] p.data1;
229+
p.data1[0] = 0; // BAD
230+
p.data2[0] = 0; // GOOD
231+
232+
p.data1 = new char[128];
233+
p.data1[0] = 0; // GOOD
234+
p.data2[0] = 0; // GOOD
235+
236+
delete [] p.data1;
237+
delete [] p.data2;
221238
}

0 commit comments

Comments
 (0)