Skip to content

Commit f1d2dac

Browse files
committed
C++: Fix a bug where the destructor attached to a 'new' expression would
have multiple parents (the 'new' expression, the call to 'operator new', and the size expression). This happens because the latter two are 'TranslatedExpr's that return the 'new' expression as their expression even though they don't technically represent the translation of this expression. To prevent this bug we tell the IR construction that the latter two handle their destructors explicitly which means that IR construction doesn't try to synthesize them.
1 parent b042366 commit f1d2dac

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,13 @@ abstract class TranslatedAllocationSize extends TranslatedExpr, TTranslatedAlloc
20152015
final override predicate producesExprResult() { none() }
20162016

20172017
final override Instruction getResult() { result = this.getInstruction(AllocationSizeTag()) }
2018+
2019+
final override predicate handlesDestructorsExplicitly() {
2020+
// Since the enclosing `TranslatedNewOrNewArrayExpr` (implicitly) handles the destructors
2021+
// we need to disable the implicit handling here as otherwise the destructors will have
2022+
// multiple parents
2023+
any()
2024+
}
20182025
}
20192026

20202027
TranslatedAllocationSize getTranslatedAllocationSize(NewOrNewArrayExpr newExpr) {
@@ -2172,6 +2179,13 @@ class TranslatedAllocatorCall extends TTranslatedAllocatorCall, TranslatedDirect
21722179

21732180
final override predicate producesExprResult() { none() }
21742181

2182+
final override predicate handlesDestructorsExplicitly() {
2183+
// Since the enclosing `TranslatedNewOrNewArrayExpr` (implicitly) handles the destructors
2184+
// we need to disable the implicit handling here as otherwise the destructors will have
2185+
// multiple parents
2186+
any()
2187+
}
2188+
21752189
override Function getInstructionFunction(InstructionTag tag) {
21762190
tag = CallTargetTag() and result = expr.getAllocator()
21772191
}

0 commit comments

Comments
 (0)