Skip to content

Commit dc4160b

Browse files
committed
Rust: Prune CFG for impossible true/false edges
1 parent bf58bdd commit dc4160b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,21 @@ abstract class ConditionalCompletion extends NormalCompletion {
6767
abstract ConditionalCompletion getDual();
6868
}
6969

70+
/** Holds if node `n` has the Boolean constant value `value`. */
71+
private predicate isBooleanConstant(AstNode n, Boolean value) {
72+
n.(LiteralExpr).getTextValue() = value.toString()
73+
or
74+
isBooleanConstant(n.(ParenExpr).getExpr(), value)
75+
}
76+
7077
/**
7178
* A completion that represents evaluation of an expression
7279
* with a Boolean value.
7380
*/
7481
class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
7582
BooleanCompletion() { this = TBooleanCompletion(value) }
7683

77-
override predicate isValidForSpecific(AstNode e) {
84+
private predicate isValidForSpecific0(AstNode e) {
7885
e = any(IfExpr c).getCondition()
7986
or
8087
any(MatchArm arm).getGuard() = e
@@ -84,7 +91,7 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
8491
e = expr.getLhs()
8592
)
8693
or
87-
exists(Expr parent | this.isValidForSpecific(parent) |
94+
exists(Expr parent | this.isValidForSpecific0(parent) |
8895
parent =
8996
any(PrefixExpr expr |
9097
expr.getOperatorName() = "!" and
@@ -103,6 +110,15 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
103110
)
104111
}
105112

113+
override predicate isValidForSpecific(AstNode e) {
114+
this.isValidForSpecific0(e) and
115+
(
116+
isBooleanConstant(e, value)
117+
or
118+
not isBooleanConstant(e, _)
119+
)
120+
}
121+
106122
/** Gets the dual Boolean completion. */
107123
override BooleanCompletion getDual() { result = TBooleanCompletion(value.booleanNot()) }
108124

rust/ql/test/library-tests/controlflow/Cfg.expected

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,8 @@
262262
| test.rs:146:1:151:1 | enter dead_code | test.rs:147:5:149:5 | ExprStmt | |
263263
| test.rs:146:1:151:1 | exit dead_code (normal) | test.rs:146:1:151:1 | exit dead_code | |
264264
| test.rs:147:5:149:5 | ExprStmt | test.rs:147:9:147:12 | true | |
265-
| test.rs:147:5:149:5 | IfExpr | test.rs:150:5:150:13 | ExprStmt | |
266-
| test.rs:147:8:147:13 | ParenExpr | test.rs:147:5:149:5 | IfExpr | false |
267265
| test.rs:147:8:147:13 | ParenExpr | test.rs:148:9:148:17 | ExprStmt | true |
268266
| test.rs:147:9:147:12 | true | test.rs:147:8:147:13 | ParenExpr | |
269267
| test.rs:148:9:148:16 | ReturnExpr | test.rs:146:1:151:1 | exit dead_code (normal) | return |
270268
| test.rs:148:9:148:17 | ExprStmt | test.rs:148:16:148:16 | 0 | |
271269
| test.rs:148:16:148:16 | 0 | test.rs:148:9:148:16 | ReturnExpr | |
272-
| test.rs:150:5:150:12 | ReturnExpr | test.rs:146:1:151:1 | exit dead_code (normal) | return |
273-
| test.rs:150:5:150:13 | ExprStmt | test.rs:150:12:150:12 | 1 | |
274-
| test.rs:150:12:150:12 | 1 | test.rs:150:5:150:12 | ReturnExpr | |

0 commit comments

Comments
 (0)