Skip to content

Commit 8c956e8

Browse files
committed
Rust: Add LogicalOperation.qll
1 parent 3bd5c6e commit 8c956e8

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class BecomeExprTree extends StandardPostOrderTree instanceof BecomeExpr {
7373
}
7474

7575
class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
76-
BinaryOpExprTree() { super.getOperatorName() != "&&" and super.getOperatorName() != "||" }
76+
BinaryOpExprTree() { not this instanceof LogicalOrExpr and not this instanceof LogicalAndExpr }
7777

7878
override ControlFlowTree getChildNode(int i) {
7979
i = 0 and result = super.getLhs()
@@ -82,61 +82,53 @@ class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
8282
}
8383
}
8484

85-
class LogicalOrBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
86-
LogicalOrBinaryOpExprTree() { super.getOperatorName() = "||" }
87-
88-
final override predicate propagatesAbnormal(AstNode child) {
89-
child = [super.getRhs(), super.getLhs()]
90-
}
85+
class LogicalOrBinaryOpExprTree extends PreOrderTree, LogicalOrExpr {
86+
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
9187

9288
override predicate succ(AstNode pred, AstNode succ, Completion c) {
9389
// Edge to the first node in the lhs
9490
pred = this and
95-
first(super.getLhs(), succ) and
91+
first(this.getLhs(), succ) and
9692
completionIsSimple(c)
9793
or
9894
// Edge from the last node in the lhs to the first node in the rhs
99-
last(super.getLhs(), pred, c) and
100-
first(super.getRhs(), succ) and
95+
last(this.getLhs(), pred, c) and
96+
first(this.getRhs(), succ) and
10197
c.(BooleanCompletion).failed()
10298
}
10399

104100
override predicate last(AstNode node, Completion c) {
105101
// Lhs. as the last node
106-
last(super.getLhs(), node, c) and
102+
last(this.getLhs(), node, c) and
107103
c.(BooleanCompletion).succeeded()
108104
or
109105
// Rhs. as the last node
110-
last(super.getRhs(), node, c)
106+
last(this.getRhs(), node, c)
111107
}
112108
}
113109

114-
class LogicalAndBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
115-
LogicalAndBinaryOpExprTree() { super.getOperatorName() = "&&" }
116-
117-
final override predicate propagatesAbnormal(AstNode child) {
118-
child = [super.getRhs(), super.getLhs()]
119-
}
110+
class LogicalAndBinaryOpExprTree extends PreOrderTree, LogicalAndExpr {
111+
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
120112

121113
override predicate succ(AstNode pred, AstNode succ, Completion c) {
122114
// Edge to the first node in the lhs
123115
pred = this and
124-
first(super.getLhs(), succ) and
116+
first(this.getLhs(), succ) and
125117
completionIsSimple(c)
126118
or
127119
// Edge from the last node in the lhs to the first node in the rhs
128-
last(super.getLhs(), pred, c) and
129-
first(super.getRhs(), succ) and
120+
last(this.getLhs(), pred, c) and
121+
first(this.getRhs(), succ) and
130122
c.(BooleanCompletion).succeeded()
131123
}
132124

133125
override predicate last(AstNode node, Completion c) {
134126
// Lhs. as the last node
135-
last(super.getLhs(), node, c) and
127+
last(this.getLhs(), node, c) and
136128
c.(BooleanCompletion).failed()
137129
or
138130
// Rhs. as the last node
139-
last(super.getRhs(), node, c)
131+
last(this.getRhs(), node, c)
140132
}
141133
}
142134

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
private import codeql.rust.elements.Expr
2+
private import codeql.rust.elements.BinaryExpr
3+
private import codeql.rust.elements.PrefixExpr
4+
5+
abstract private class LogicalOperationImpl extends Expr {
6+
abstract Expr getAnOperand();
7+
}
8+
9+
final class LogicalOperation = LogicalOperationImpl;
10+
11+
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl {
12+
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
13+
}
14+
15+
final class BinaryLogicalOperation = BinaryLogicalOperationImpl;
16+
17+
final class LogicalAndExpr extends BinaryLogicalOperationImpl, BinaryExpr {
18+
LogicalAndExpr() { this.getOperatorName() = "&&" }
19+
}
20+
21+
final class LogicalOrExpr extends BinaryLogicalOperationImpl {
22+
LogicalOrExpr() { this.getOperatorName() = "||" }
23+
}
24+
25+
abstract private class UnaryLogicalOperationImpl extends PrefixExpr, LogicalOperationImpl { }
26+
27+
final class UnaryLogicalOperation = UnaryLogicalOperationImpl;
28+
29+
final class LogicalNotExpr extends UnaryLogicalOperationImpl {
30+
LogicalNotExpr() { this.getOperatorName() = "!" }
31+
32+
override Expr getAnOperand() { result = this.getExpr() }
33+
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
import codeql.rust.elements
44
import codeql.Locations
55
import codeql.files.FileSystem
6+
import codeql.rust.elements.LogicalOperation

0 commit comments

Comments
 (0)