Skip to content

Commit eb0621a

Browse files
authored
Merge pull request #16406 from geoffw0/test1
C++: Add test case for reassignment to UseAfterFree.ql.
2 parents c0cf1c7 + 315f439 commit eb0621a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
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 |
2931
| test_free.cpp:11:10:11:10 | a |
3032
| test_free.cpp:14:10:14:10 | a |
3133
| 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,4 +1,6 @@
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 | |
24
| test_free.cpp:11:10:11:10 | pointer to free output argument | test_free.cpp:12:5:12:5 | a | provenance | |
35
| test_free.cpp:11:10:11:10 | pointer to free output argument | test_free.cpp:13:5:13:6 | * ... | provenance | |
46
| test_free.cpp:42:27:42:27 | pointer to free output argument | test_free.cpp:45:5:45:5 | a | provenance | |
@@ -31,6 +33,10 @@ edges
3133
| test_free.cpp:322:12:322:12 | pointer to operator delete output argument | test_free.cpp:324:5:324:6 | * ... | provenance | |
3234
| test_free.cpp:331:12:331:12 | pointer to operator delete output argument | test_free.cpp:332:5:332:6 | * ... | provenance | |
3335
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 |
3440
| test_free.cpp:11:10:11:10 | pointer to free output argument | semmle.label | pointer to free output argument |
3541
| test_free.cpp:12:5:12:5 | a | semmle.label | a |
3642
| test_free.cpp:13:5:13:6 | * ... | semmle.label | * ... |
@@ -82,6 +88,8 @@ nodes
8288
| test_free.cpp:332:5:332:6 | * ... | semmle.label | * ... |
8389
subpaths
8490
#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 |
8593
| 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 |
8694
| 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 |
8795
| 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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ int main()
114114
mc2->method2();
115115
delete mc2;
116116
}
117-
117+
118118
{
119119
void *v1 = malloc(100);
120120
int *i2 = (int *)malloc(100);
@@ -198,3 +198,19 @@ void test_strndupa_dealloc() {
198198
char *cpy = strndupa(msg, 4);
199199
free(cpy); // BAD [NOT DETECTED]
200200
}
201+
202+
// ---
203+
204+
void test_reassignment() {
205+
char *a = (char *)malloc(128);
206+
char *b = (char *)malloc(128);
207+
208+
free(a);
209+
a[0] = 0; // BAD
210+
211+
a = b;
212+
a[0] = 0; // GOOD
213+
214+
free(a);
215+
a[0] = 0; // BAD
216+
}

0 commit comments

Comments
 (0)