Skip to content

C++: Fix asExpr for TemporaryObjectExpr - #23349

Open
MathiasVP wants to merge 3 commits into
github:mainfrom
MathiasVP:dont-map-temporary-reuse-in-as-expr
Open

MathiasVP wants to merge 3 commits into
github:mainfrom
MathiasVP:dont-map-temporary-reuse-in-as-expr

Conversation

@MathiasVP

Copy link
Copy Markdown
Contributor

For a temporary of a class with a destructor (such as return_obj_by_value().f()), the extractor generates a TemporaryObjectExpr for the qualifier of f() and a ReuseExpr to reuse it in the implicit destructor call:

r1 = VariableAddress[#temp] // TemporaryObjectExpr
r3 = Call[return_obj_by_value]
m6 = Store[#temp] &:r1, r3
...
v9 = Call[f] this:r1
...
r15 = CopyValue r1 // ReuseExpr
v17 = Call[~Obj] this:r15

We want the "most converted instruction" to be the instruction for which asExpr has a result. To do this, getConvertedResultExpression has a column n to indicate the index in a chain of conversions. Because r15 is a CopyValue, getConvertedResultExpression treats it as a conversion of r1. So r15 gets ReuseExpr at n = 0 and return_obj_by_value() at n = 1, while r1 keeps return_obj_by_value() at n = 0. As a result asExpr maps return_obj_by_value() to 2 nodes: the qualifiers of f() and ~Obj().

This PR introduces exprConversionFlow, which excludes ReuseExpr when the reused value is accessed elsewhere and treats the store initializing a TemporaryObjectExpr (m6 -> r1) as a conversion step.

Commit-by-commit review recommended.

@MathiasVP
MathiasVP requested a review from a team as a code owner December 3, 2026 12:17
Copilot AI adaptive review requested due to automatic review settings December 3, 2026 12:17
@MathiasVP MathiasVP added C++ no-change-note-required This PR does not need a change note labels Dec 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Interacting changes to conversion ranking, temporary initialization, destructor reuse, and node selection alter expression-to-node identity across C++ dataflow analyses beyond the limited-consequence review scope.

No open issues 🎉

What changed in this PR

Fixes C++ dataflow expression mapping for temporary objects, avoiding duplicate asExpr mappings caused by implicit destructor calls.

Changes:

  • Refines conversion flow to handle temporary initialization and reused values.
  • Adds regression cases for temporaries with and without destructors.
File Description
cpp/​ql/​test/​library-tests/​dataflow/​asExpr/​test.cpp Adds temporary-object expression mapping tests.
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​implementation/​raw/​internal/​IRConstruction.qll Links initialization stores to temporary-object expressions.
cpp/​ql/​lib/​semmle/​code/​cpp/​ir/​dataflow/​internal/​ExprNodes.qll Refines conversion steps used for expression mapping.

Review effort: Balanced (adaptive)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants