diff --git a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll index bc433942506b..3ffdc7cfb903 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll @@ -15,6 +15,49 @@ private module Cached { result = any(InitializeDynamicAllocationInstruction init).getAllocationAddressOperand() } + /** Holds if `instr` computes the result of a `ReuseExpr`. */ + private predicate isReuse(Instruction instr) { + instr.getUnconvertedResultExpression() instanceof ReuseExpr + } + + /** + * Holds if `op` is the operand of an instruction representing a + * `ReuseExpr`, and the reused value is also accessed elsewhere. + */ + private predicate isReuseOfAccessedValue(Operand op) { + isReuse(op.getUse()) and + exists(Operand other | + other = getAUse(op.getDef()) and + other != op and + not other = any(StoreInstruction store).getDestinationAddressOperand() + ) + } + + /** + * Holds if `opFrom` is the source operand of the `StoreInstruction` that + * initializes a temporary object, and `instrTo` computes the address of the + * temporary. + */ + private predicate initializesTemporaryObject(Operand opFrom, Instruction instrTo) { + exists(StoreInstruction store | + opFrom = store.getSourceValueOperand() and + instrTo = store.getDestinationAddress() and + instrTo.getConvertedResultExpression() = + IRConstruction::Raw::getTemporaryObjectInitializationStoreExpr(store) + ) + } + + /** + * Holds if `opFrom` flows to `instrTo` through a step that corresponds to a + * conversion in the AST. + */ + private predicate exprConversionFlow(Operand opFrom, Instruction instrTo) { + conversionFlow(opFrom, instrTo, false, false) and + not isReuseOfAccessedValue(opFrom) + or + initializesTemporaryObject(opFrom, instrTo) + } + /** * Gets the expression that should be returned as the result expression from `instr`. * @@ -23,7 +66,7 @@ private module Cached { */ private Expr getConvertedResultExpression(Instruction instr, int n) { // Only fully converted instructions have a result for `asConvertedExpr` - not conversionFlow(unique(Operand op | + not exprConversionFlow(unique(Operand op | // The address operand of a `InitializeDynamicAllocationInstruction` is // special: we need to handle it during dataflow (since it's // effectively a store to an indirection), but it doesn't appear in @@ -32,12 +75,12 @@ private module Cached { op = getAUse(instr) and not op = getAnInitializeDynamicAllocationInstructionAddress() | op - ), _, false, false) and + ), _) and result = getConvertedResultExpressionImpl(instr) and n = 0 or // If the conversion also has a result then we return multiple results - exists(Operand operand | conversionFlow(operand, instr, false, false) | + exists(Operand operand | exprConversionFlow(operand, instr) | n = 1 and result = getConvertedResultExpressionImpl(operand.getDef()) or diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll index da8c394c845c..abdfd829d958 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll @@ -276,6 +276,18 @@ module Raw { ) } + /** + * Gets the `TemporaryObjectExpr` whose temporary object is initialized by + * `store`. + */ + cached + TemporaryObjectExpr getTemporaryObjectInitializationStoreExpr(StoreInstruction store) { + exists(TranslatedTemporaryObjectExpr ttoe | + store = ttoe.getInitialization().getInstruction(InitializerStoreTag()) and + result = ttoe.getExpr() + ) + } + /** Gets the instruction that computes the address used to initialize `v`. */ cached Instruction getInitializationTargetAddress(IRVariable v) { diff --git a/cpp/ql/test/library-tests/dataflow/asExpr/test.cpp b/cpp/ql/test/library-tests/dataflow/asExpr/test.cpp index 5aed0c3f4ea2..4d1e2657d7b1 100644 --- a/cpp/ql/test/library-tests/dataflow/asExpr/test.cpp +++ b/cpp/ql/test/library-tests/dataflow/asExpr/test.cpp @@ -73,3 +73,22 @@ void test_parameters( int *p, // $ asParameter=p parameterNode=p int *&rp) { // $ asParameter=rp(*rp) parameterNode=rp(*rp) } + +struct NoDestructor { + void f(); +}; + +NoDestructor get_no_destructor(); + +struct HasDestructor { + ~HasDestructor(); + void f(); +}; + +HasDestructor get_has_destructor(); + +void test_temporaries() { + get_no_destructor().f(); // $ asExpr="call to get_no_destructor" asIndirectExpr="call to get_no_destructor" + get_has_destructor().f(); // $ asExpr="call to get_has_destructor" asExpr="reuse of temporary object" asIndirectExpr="call to get_has_destructor" asIndirectExpr="reuse of temporary object" + get_has_destructor(); // $ asExpr="call to get_has_destructor(reuse of temporary object)" asExpr="reuse of temporary object" asIndirectExpr="call to get_has_destructor(reuse of temporary object)" asIndirectExpr="reuse of temporary object" +}