Skip to content

Commit 741a328

Browse files
authored
Merge pull request #17055 from geoffw0/usefree
C++: More tests for cpp/use-after-free
2 parents 4cbc334 + 1456012 commit 741a328

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
| test.cpp:128:15:128:16 | v4 |
2727
| test.cpp:185:10:185:12 | cpy |
2828
| test.cpp:199:10:199:12 | cpy |
29-
| test.cpp:208:7:208:7 | a |
30-
| test.cpp:214:7:214:7 | a |
29+
| test.cpp:213:7:213:7 | a |
30+
| 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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
edges
2-
| test.cpp:208:7:208:7 | pointer to free output argument | test.cpp:209:2:209:2 | a | provenance | |
3-
| test.cpp:214:7:214:7 | pointer to free output argument | test.cpp:215:2:215:2 | a | provenance | |
2+
| test.cpp:213:7:213:7 | pointer to free output argument | test.cpp:214:2:214:2 | a | provenance | |
3+
| 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 | |
@@ -33,10 +36,14 @@ edges
3336
| test_free.cpp:322:12:322:12 | pointer to operator delete output argument | test_free.cpp:324:5:324:6 | * ... | provenance | |
3437
| test_free.cpp:331:12:331:12 | pointer to operator delete output argument | test_free.cpp:332:5:332:6 | * ... | provenance | |
3538
nodes
36-
| test.cpp:208:7:208:7 | pointer to free output argument | semmle.label | pointer to free output argument |
37-
| test.cpp:209:2:209:2 | a | semmle.label | a |
38-
| test.cpp:214:7:214:7 | pointer to free output argument | semmle.label | pointer to free output argument |
39-
| test.cpp:215:2:215:2 | a | semmle.label | a |
39+
| test.cpp:213:7:213:7 | pointer to free output argument | semmle.label | pointer to free output argument |
40+
| test.cpp:214:2:214:2 | a | semmle.label | a |
41+
| test.cpp:219:7:219:7 | pointer to free output argument | semmle.label | pointer to free output argument |
42+
| 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 | * ... |
@@ -88,8 +95,9 @@ nodes
8895
| test_free.cpp:332:5:332:6 | * ... | semmle.label | * ... |
8996
subpaths
9097
#select
91-
| test.cpp:209:2:209:2 | a | test.cpp:208:7:208:7 | pointer to free output argument | test.cpp:209:2:209:2 | a | Memory may have been previously freed by $@. | test.cpp:208:2:208:5 | call to free | call to free |
92-
| test.cpp:215:2:215:2 | a | test.cpp:214:7:214:7 | pointer to free output argument | test.cpp:215:2:215:2 | a | Memory may have been previously freed by $@. | test.cpp:214:2:214:5 | call to free | call to free |
98+
| 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 |
99+
| 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ void test_strndupa_dealloc() {
201201

202202
// ---
203203

204+
struct DataPair {
205+
char *data1;
206+
char *data2;
207+
};
208+
204209
void test_reassignment() {
205210
char *a = (char *)malloc(128);
206211
char *b = (char *)malloc(128);
@@ -213,4 +218,21 @@ void test_reassignment() {
213218

214219
free(a);
215220
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;
216238
}

0 commit comments

Comments
 (0)