Skip to content

Commit 3e9f964

Browse files
committed
C++: Exclude calls to operator new / delete from NewFreeMismatch.ql.
1 parent 97cdcbe commit 3e9f964

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import cpp
66
import semmle.code.cpp.controlflow.SSA
77
import semmle.code.cpp.dataflow.DataFlow
8+
import semmle.code.cpp.models.implementations.Allocation
9+
import semmle.code.cpp.models.implementations.Deallocation
810

911
/**
1012
* Holds if `alloc` is a use of `malloc` or `new`. `kind` is
@@ -15,6 +17,7 @@ predicate allocExpr(Expr alloc, string kind) {
1517
not alloc.isFromUninstantiatedTemplate(_) and
1618
(
1719
alloc instanceof FunctionCall and
20+
not alloc.(FunctionCall).getTarget() instanceof OperatorNewAllocationFunction and
1821
kind = "malloc"
1922
or
2023
alloc instanceof NewExpr and
@@ -111,6 +114,7 @@ predicate allocReaches(Expr e, Expr alloc, string kind) {
111114
*/
112115
predicate freeExpr(Expr free, Expr freed, string kind) {
113116
freeCall(free, freed) and
117+
not free.(FunctionCall).getTarget() instanceof OperatorDeleteDeallocationFunction and
114118
kind = "free"
115119
or
116120
free.(DeleteExpr).getExpr() = freed and

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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 |
3-
| test2.cpp:50:2:50:18 | call to operator delete | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:45:18:45:24 | new | new |
43
| 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 |
5-
| test2.cpp:53:2:53:17 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test2.cpp:46:20:46:33 | call to operator new | malloc |
64
| 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 |
75
| 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 |
86
| 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 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ void test_operator_new()
4747
void *ptr_malloc = malloc(sizeof(int));
4848

4949
delete ptr_new; // GOOD
50-
::operator delete(ptr_new); // GOOD [FALSE POSITIVE]
50+
::operator delete(ptr_new); // GOOD
5151
free(ptr_new); // BAD
5252

53-
delete ptr_opnew; // GOOD [FALSE POSITIVE]
53+
delete ptr_opnew; // GOOD
5454
::operator delete(ptr_opnew); // GOOD
5555
free(ptr_opnew); // BAD [NOT DETECTED]
5656

0 commit comments

Comments
 (0)