Skip to content

Commit 8059d69

Browse files
committed
C++: Model calls to operator new / delete for NewFreeMismatch.ql.
1 parent 3e9f964 commit 8059d69

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@ predicate allocExpr(Expr alloc, string kind) {
1616
isAllocationExpr(alloc) and
1717
not alloc.isFromUninstantiatedTemplate(_) and
1818
(
19-
alloc instanceof FunctionCall and
20-
not alloc.(FunctionCall).getTarget() instanceof OperatorNewAllocationFunction and
21-
kind = "malloc"
19+
exists(Function target |
20+
alloc.(FunctionCall).getTarget() = target and
21+
(
22+
target.getName() = "operator new" and
23+
kind = "new"
24+
or
25+
target.getName() = "operator new[]" and
26+
kind = "new[]"
27+
or
28+
not target instanceof OperatorNewAllocationFunction and
29+
kind = "malloc"
30+
)
31+
)
2232
or
2333
alloc instanceof NewExpr and
2434
kind = "new" and
@@ -113,9 +123,20 @@ predicate allocReaches(Expr e, Expr alloc, string kind) {
113123
* describing the type of that free or delete.
114124
*/
115125
predicate freeExpr(Expr free, Expr freed, string kind) {
116-
freeCall(free, freed) and
117-
not free.(FunctionCall).getTarget() instanceof OperatorDeleteDeallocationFunction and
118-
kind = "free"
126+
exists(Function target |
127+
freeCall(free, freed) and
128+
free.(FunctionCall).getTarget() = target and
129+
(
130+
target.getName() = "operator delete" and
131+
kind = "delete"
132+
or
133+
target.getName() = "operator delete[]" and
134+
kind = "delete[]"
135+
or
136+
not target instanceof OperatorDeleteDeallocationFunction and
137+
kind = "free"
138+
)
139+
)
119140
or
120141
free.(DeleteExpr).getExpr() = freed and
121142
kind = "delete"

cpp/ql/test/query-tests/Critical/NewFree/NewFreeMismatch.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
| test2.cpp:19:3:19:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:18:12:18:18 | new | new |
22
| test2.cpp:26:3:26:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:25:7:25:13 | new | new |
33
| test2.cpp:51:2:51:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:45:18:45:24 | new | new |
4+
| test2.cpp:55:2:55:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:46:20:46:33 | call to operator new | new |
45
| test2.cpp:57:2:57:18 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test2.cpp:47:21:47:26 | call to malloc | malloc |
6+
| test2.cpp:58:2:58:18 | call to operator delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test2.cpp:47:21:47:26 | call to malloc | malloc |
57
| test.cpp:36:2:36:17 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:27:18:27:23 | call to malloc | malloc |
68
| test.cpp:41:2:41:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:26:7:26:17 | new | new |
79
| test.cpp:68:3:68:11 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:64:28:64:33 | call to malloc | malloc |

cpp/ql/test/query-tests/Critical/NewFree/test2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void test_operator_new()
5252

5353
delete ptr_opnew; // GOOD
5454
::operator delete(ptr_opnew); // GOOD
55-
free(ptr_opnew); // BAD [NOT DETECTED]
55+
free(ptr_opnew); // BAD
5656

5757
delete ptr_malloc; // BAD
58-
::operator delete(ptr_malloc); // BAD [NOT DETECTED]
58+
::operator delete(ptr_malloc); // BAD
5959
free(ptr_malloc); // GOOD
6060
}

0 commit comments

Comments
 (0)