Skip to content

Commit 8595776

Browse files
committed
Rust: Fix CFG for while let loops
1 parent 0710768 commit 8595776

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class IfExprTree extends PostOrderTree instanceof IfExpr {
193193
child = [super.getCondition(), super.getThen(), super.getElse()]
194194
}
195195

196-
ConditionalCompletion conditionCompletion(Completion c) {
196+
private ConditionalCompletion conditionCompletion(Completion c) {
197197
if super.getCondition() instanceof LetExpr
198198
then result = c.(MatchCompletion)
199199
else result = c.(BooleanCompletion)
@@ -345,15 +345,21 @@ class WhileExprTree extends LoopingExprTree instanceof WhileExpr {
345345

346346
override predicate first(AstNode node) { first(super.getCondition(), node) }
347347

348+
private ConditionalCompletion conditionCompletion(Completion c) {
349+
if super.getCondition() instanceof LetExpr
350+
then result = c.(MatchCompletion)
351+
else result = c.(BooleanCompletion)
352+
}
353+
348354
override predicate succ(AstNode pred, AstNode succ, Completion c) {
349355
super.succ(pred, succ, c)
350356
or
351357
last(super.getCondition(), pred, c) and
352-
c.(BooleanCompletion).succeeded() and
358+
this.conditionCompletion(c).succeeded() and
353359
first(this.getLoopBody(), succ)
354360
or
355361
last(super.getCondition(), pred, c) and
356-
c.(BooleanCompletion).failed() and
362+
this.conditionCompletion(c).failed() and
357363
succ = this
358364
}
359365

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,26 @@
123123
| test.rs:61:13:61:22 | ExprStmt | test.rs:61:13:61:13 | PathExpr | |
124124
| test.rs:61:17:61:21 | false | test.rs:61:13:61:21 | ... = ... | |
125125
| test.rs:65:5:72:5 | enter test_while_let | test.rs:66:9:66:29 | LetStmt | |
126+
| test.rs:65:5:72:5 | exit test_while_let (normal) | test.rs:65:5:72:5 | exit test_while_let | |
127+
| test.rs:65:25:72:5 | BlockExpr | test.rs:65:5:72:5 | exit test_while_let (normal) | |
126128
| test.rs:66:9:66:29 | LetStmt | test.rs:66:24:66:24 | 1 | |
127129
| test.rs:66:13:66:20 | iter | test.rs:67:15:67:39 | LetExpr | match, no-match |
128130
| test.rs:66:24:66:24 | 1 | test.rs:66:27:66:28 | 10 | |
129131
| test.rs:66:24:66:28 | RangeExpr | test.rs:66:13:66:20 | iter | |
130132
| test.rs:66:27:66:28 | 10 | test.rs:66:24:66:28 | RangeExpr | |
133+
| test.rs:67:9:71:9 | WhileExpr | test.rs:65:25:72:5 | BlockExpr | |
131134
| test.rs:67:15:67:39 | LetExpr | test.rs:67:19:67:25 | TupleStructPat | |
135+
| test.rs:67:19:67:25 | TupleStructPat | test.rs:67:9:71:9 | WhileExpr | no-match |
136+
| test.rs:67:19:67:25 | TupleStructPat | test.rs:68:17:68:17 | PathExpr | match |
137+
| test.rs:67:41:71:9 | BlockExpr | test.rs:67:15:67:39 | LetExpr | |
138+
| test.rs:68:13:70:13 | IfExpr | test.rs:67:41:71:9 | BlockExpr | |
139+
| test.rs:68:16:68:22 | ParenExpr | test.rs:68:13:70:13 | IfExpr | false |
140+
| test.rs:68:16:68:22 | ParenExpr | test.rs:69:17:69:22 | ExprStmt | true |
141+
| test.rs:68:17:68:17 | PathExpr | test.rs:68:21:68:21 | 5 | |
142+
| test.rs:68:17:68:21 | ... = ... | test.rs:68:16:68:22 | ParenExpr | |
143+
| test.rs:68:21:68:21 | 5 | test.rs:68:17:68:21 | ... = ... | |
144+
| test.rs:69:17:69:21 | BreakExpr | test.rs:67:9:71:9 | WhileExpr | break |
145+
| test.rs:69:17:69:22 | ExprStmt | test.rs:69:17:69:21 | BreakExpr | |
132146
| test.rs:74:5:81:5 | enter test_for | test.rs:75:18:75:18 | 0 | |
133147
| test.rs:74:5:81:5 | exit test_for (normal) | test.rs:74:5:81:5 | exit test_for | |
134148
| test.rs:74:25:81:5 | BlockExpr | test.rs:74:5:81:5 | exit test_for (normal) | |

0 commit comments

Comments
 (0)