Repository navigation
C++: recognize assignment helpers returning reference parameters - #23109
Open
SkyCipherr wants to merge 1 commit into
Open
SkyCipherr wants to merge 1 commit into
SkyCipherr wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Explicit casts in helper returns and arguments can suppress genuine assignment alerts by selecting a different base subobject, and final-change DCA validation remains outstanding.
3 open issues
What changed in this PR
This PR addresses false positives from issue #7035 by extending the C++ assignment-return query to recognize helpers that return a destination reference parameter.
Changes:
- Adds helper summaries and maps returned parameters to arguments recognized as
*this. - Adds 16 regression cases covering supported helpers and negative controls, with updated expected results.
- Documents the analysis improvement in a change note.
| File | Description |
|---|---|
cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/helpers.cpp |
Adds safe helper cases and negative controls. |
cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.expected |
Records alerts for the eight new negative controls. |
cpp/ql/src/jsf/4.10 Classes/AV Rule 82.ql |
Adds reference-parameter helper recognition. |
cpp/ql/src/change-notes/2026-11-12-assignment-helper-return.md |
Documents the reduction in false positives. |
Review effort: Balanced (auto)
| p.getType().getUnspecifiedType().(ReferenceType).getBaseType().getUnspecifiedType() and | ||
| exists(ReturnStmt s | s.getEnclosingFunction() = f and reachable(s)) and | ||
| forall(ReturnStmt s | s.getEnclosingFunction() = f and reachable(s) | | ||
| s.getExpr().getUnconverted().(VariableAccess).getTarget() = p |
Comment on lines
+71
to
+73
| arg.getUnconverted().getType().getUnspecifiedType() = | ||
| p.getType().getUnspecifiedType().(ReferenceType).getBaseType().getUnspecifiedType() and | ||
| dereferenceThis(arg) |
Contributor
|
Hi @SkyCipherr, Observe that the jsf rules are a very specific set of coding standard rules, see, e.g., https://www.stroustrup.com/JSF-AV-rules.pdf. I'll need to discuss internally, but this might no longer be in the spirit of the AV 82 rule. |
Contributor
|
@jketema I'm happy with this, assuming DCA doesn't throw up any surprises. 👍 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Related to #7035.
The
cpp/assignment-does-not-return-thisquery can report an assignment operator that returnshelper(*this, other), even when the helper returns its destination parameter by reference on every reachable return.Add a small reference-parameter summary for helpers with visible, non-virtual definitions. The summary requires a reference parameter and reference return type with matching base types, at least one reachable return, and direct accesses to the same parameter at every reachable return. At the call site, map the returned parameter to its argument, require matching unconverted argument and reference-base types, and use the existing
dereferenceThispredicate to recognize the destination.This handles free and static member helpers, instantiated templates, parameters at different positions, multiple explicit returns, and unreachable alternative returns. It does not infer general alias relationships or follow helper return expressions through casts, local aliases, nested calls, or conditional expressions. Virtual helpers and unavailable definitions remain conservative.
Validation
Validated with CodeQL CLI 2.27.4 against main at
fbf9fca5ce7c3b64c2f5d719ea0444577850cb4a:AV Rule 82.qlrefregression test passes, including inline expectations.git diff --checkpass.The full repository suite, a full build of the original cxon project, and large-project evaluation were not run. Upstream CI has not yet run for this change.