Repository navigation
Rust: Do not treat the early return of ? as returning the unwrapped value - #22903
Open
LucaCappelletti94 wants to merge 2 commits into
Open
LucaCappelletti94 wants to merge 2 commits into
LucaCappelletti94 wants to merge 2 commits into
Conversation
| exists(SuccessorType t | n.getACfgNode().getASuccessor(t) instanceof AnnotatedExitCfgNode | | ||
| // The early return of `x?` returns the `Err` or `None` held by `x`, which is | ||
| // a different value from the one `x?` evaluates to | ||
| not (n instanceof TryExpr and t instanceof ReturnSuccessor) |
Contributor
Author
There was a problem hiding this comment.
Both sites now use a shared isReturnedValue predicate, with a callback test in dataflow/models.
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.

ExprReturnNodetreats any expression with a control-flow edge into the function exit as a returned value. The?operator has such an edge for its early return, so everyx?became a return node, and the unwrappedOkorSomevalue flowed out of the enclosing function as though it were the function's result. A function that reads a value with?and then returns something unrelated therefore passed that value on to its callers.This surfaced as
rust/cleartext-loggingfalse positives on real code, where a helper read a secret with?and the alert followed it through several callers to anassert!that printed an unrelated struct. The?expression now stays a return node only along its normal edge, which keepsx?in tail position. The early return itself, theErrorNoneheld byx, was not modelled as returned data before and still is not, so no existing flow is lost.I am not too familiar with editing CodeQL so I relied on Opus for parts of the PR + testing the hell out of the changes, I hope all looks decent.