Skip to content

Commit

Permalink
Merge pull request #17749 from paldepind/rust-cfg-handle-question-mark
Browse files Browse the repository at this point in the history
Rust: Handle question mark operator in CFG
  • Loading branch information
paldepind authored Oct 14, 2024
2 parents de61296 + e83f1d1 commit d0f978d
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 142 deletions.
12 changes: 10 additions & 2 deletions rust/ql/lib/codeql/rust/controlflow/internal/Completion.qll
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ class SimpleCompletion extends NormalCompletion, TSimpleCompletion {

// `SimpleCompletion` is the "default" completion type, thus it is valid for
// any node where there isn't another more specific completion type.
override predicate isValidFor(AstNode e) { not any(Completion c).isValidForSpecific(e) }
override predicate isValidFor(AstNode e) {
not any(Completion c).isValidForSpecific(e)
or
// A `?` expression can both proceed normally or cause an early return, so
// we explicitly allow the former here.
e instanceof TryExpr
}

override string toString() { result = "simple" }
}
Expand Down Expand Up @@ -204,7 +210,9 @@ class ContinueCompletion extends TContinueCompletion, Completion {
class ReturnCompletion extends TReturnCompletion, Completion {
override ReturnSuccessor getAMatchingSuccessorType() { any() }

override predicate isValidForSpecific(AstNode e) { e instanceof ReturnExpr }
override predicate isValidForSpecific(AstNode e) {
e instanceof ReturnExpr or e instanceof TryExpr
}

override string toString() { result = "return" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ class MatchExprTree extends PostOrderTree instanceof MatchExpr {
override predicate succ(AstNode pred, AstNode succ, Completion c) {
// Edge from the scrutinee to the first arm.
last(super.getExpr(), pred, c) and
first(super.getArm(0).getPat(), succ)
first(super.getArm(0).getPat(), succ) and
completionIsNormal(c)
or
// Edge from a failed match/guard in one arm to the beginning of the next arm.
exists(int i |
Expand Down Expand Up @@ -571,18 +572,12 @@ class RefExprTree extends StandardPostOrderTree instanceof RefExpr {
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
}

class ReturnExprTree extends PostOrderTree instanceof ReturnExpr {
override predicate propagatesAbnormal(AstNode child) { child = super.getExpr() }

override predicate first(AstNode node) {
first(super.getExpr(), node)
or
not super.hasExpr() and node = this
}
class ReturnExprTree extends StandardPostOrderTree instanceof ReturnExpr {
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
}

override predicate succ(AstNode pred, AstNode succ, Completion c) {
last(super.getExpr(), pred, c) and succ = this and completionIsNormal(c)
}
class TryExprTree extends StandardPostOrderTree instanceof TryExpr {
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
}

class TupleExprTree extends StandardPostOrderTree instanceof TupleExpr {
Expand Down
Loading

0 comments on commit d0f978d

Please sign in to comment.