Skip to content

Commit 9986206

Browse files
committed
C++: Placement new does not necessarily require a delete.
1 parent a5e10a7 commit 9986206

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

cpp/ql/src/Critical/MemoryMayNotBeFreed.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ predicate mayCallFunction(Expr call, Function f) {
2525
predicate allocCallOrIndirect(Expr e) {
2626
// direct alloc call
2727
e.(AllocationExpr).requiresDealloc() and
28-
not exists(e.(NewOrNewArrayExpr).getPlacementPointer()) and
2928
// We are only interested in alloc calls that are
3029
// actually freed somehow, as MemoryNeverFreed
3130
// will catch those that aren't.

cpp/ql/src/Critical/MemoryNeverFreed.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ import MemoryFreed
1414
from AllocationExpr alloc
1515
where
1616
alloc.requiresDealloc() and
17-
not exists(alloc.(NewOrNewArrayExpr).getPlacementPointer()) and
1817
not allocMayBeFreed(alloc)
1918
select alloc, "This memory is never freed"

cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class NewAllocationExpr extends AllocationExpr, NewExpr {
273273
NewAllocationExpr() { this instanceof NewExpr }
274274

275275
override int getSizeBytes() { result = getAllocatedType().getSize() }
276+
277+
override predicate requiresDealloc() { not exists(getPlacementPointer()) }
276278
}
277279

278280
/**
@@ -293,4 +295,6 @@ class NewArrayAllocationExpr extends AllocationExpr, NewArrayExpr {
293295
}
294296

295297
override int getSizeBytes() { result = getAllocatedType().getSize() }
298+
299+
override predicate requiresDealloc() { not exists(getPlacementPointer()) }
296300
}

cpp/ql/src/semmle/code/cpp/models/interfaces/Allocation.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ abstract class AllocationFunction extends Function {
3636

3737
/**
3838
* Whether or not this allocation requires a corresponding deallocation of
39-
* some sort (most do, but `alloca` for example does not).
39+
* some sort (most do, but `alloca` for example does not). If it is unclear,
40+
* we default to no (for example a placement `new` allocation may or may not
41+
* require a corresponding `delete`).
4042
*/
4143
predicate requiresDealloc() { any() }
4244
}
@@ -72,7 +74,9 @@ abstract class AllocationExpr extends Expr {
7274

7375
/**
7476
* Whether or not this allocation requires a corresponding deallocation of
75-
* some sort (most do, but `alloca` for example does not).
77+
* some sort (most do, but `alloca` for example does not). If it is unclear,
78+
* we default to no (for example a placement `new` allocation may or may not
79+
* require a corresponding `delete`).
7680
*/
7781
predicate requiresDealloc() { any() }
7882
}

0 commit comments

Comments
 (0)