Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Data flow through variants #18078

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,37 @@ final class RecordExprCfgNode extends Nodes::RecordExprCfgNode {

RecordExprCfgNode() { node = this.getRecordExpr() }

/** Gets the `i`th record expression. */
ExprCfgNode getExpr(int i) {
any(ChildMapping mapping)
.hasCfgChild(node, node.getRecordExprFieldList().getField(i).getExpr(), this, result)
/** Gets the record expression for the field `field`. */
pragma[nomagic]
ExprCfgNode getFieldExpr(string field) {
exists(int i |
any(ChildMapping mapping)
.hasCfgChild(node, node.getRecordExprFieldList().getField(i).getExpr(), this, result) and
field = node.getRecordExprFieldList().getField(i).getNameRef().getText()
)
}
}

/**
* A record pattern. For example:
* ```rust
* match x {
* Foo { a: 1, b: 2 } => "ok",
* Foo { .. } => "fail",
* }
* ```
*/
final class RecordPatCfgNode extends Nodes::RecordPatCfgNode {
private RecordPatChildMapping node;

RecordPatCfgNode() { node = this.getRecordPat() }

/** Gets the record pattern for the field `field`. */
PatCfgNode getFieldPat(string field) {
exists(int i |
any(ChildMapping mapping)
.hasCfgChild(node, node.getRecordPatFieldList().getField(i).getPat(), this, result) and
field = node.getRecordPatFieldList().getField(i).getNameRef().getText()
)
}
}
6 changes: 6 additions & 0 deletions rust/ql/lib/codeql/rust/controlflow/internal/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class RecordExprChildMapping extends ParentAstNode, RecordExpr {
}
}

class RecordPatChildMapping extends ParentAstNode, RecordPat {
override predicate relevantChild(AstNode child) {
child = this.getRecordPatFieldList().getAField().getPat()
}
}

class FormatArgsExprChildMapping extends ParentAstNode, CfgImpl::ExprTrees::FormatArgsExprTree {
override predicate relevantChild(AstNode child) { child = this.getChildNode(_) }
}
Expand Down
6 changes: 6 additions & 0 deletions rust/ql/lib/codeql/rust/dataflow/DataFlow.qll
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module DataFlow {

final class PostUpdateNode = Node::PostUpdateNode;

final class Content = DataFlowImpl::Content;

final class VariantContent = DataFlowImpl::VariantContent;

final class ContentSet = DataFlowImpl::ContentSet;

/**
* Holds if data flows from `nodeFrom` to `nodeTo` in exactly one local
* (intra-procedural) step.
Expand Down
Loading