Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 4882f27

Browse files
committed
Remove spurious control-flow edge around switch block without a test-expression
Previously we thought it possible to get from top to bottom of a block like "switch { case f(): ... }", when in fact this is only possible if there are no case blocks to execute. I also add tests for two possible corner cases of a switch without a test-expression: a completely empty switch (the 'true' is indeed the last node) and switch with an empty default block (a single 'skip' is generated for the default block and the 'true' is not the last node)
1 parent 1dc427a commit 4882f27

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

ql/src/semmle/go/controlflow/ControlFlowGraphImpl.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,6 @@ module CFG {
17721772
(
17731773
lastNode(this.(ExpressionSwitchStmt).getExpr(), last, cmpl)
17741774
or
1775-
last = MkImplicitTrue(this) and
1776-
cmpl = Bool(true)
1777-
or
17781775
lastNode(this.(TypeSwitchStmt).getTest(), last, cmpl)
17791776
) and
17801777
(
@@ -1783,6 +1780,10 @@ module CFG {
17831780
not exists(this.getDefault())
17841781
)
17851782
or
1783+
last = MkImplicitTrue(this) and
1784+
cmpl = Bool(true) and
1785+
this.getNumCase() = 0
1786+
or
17861787
exists(CaseClause cc, int i, Completion inner |
17871788
cc = this.getCase(i) and lastNode(cc, last, inner)
17881789
|

ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/ControlFlowNode_getASuccessor.expected

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@
12061206
| tst.go:3:12:3:12 | argument corresponding to x | tst.go:3:12:3:12 | initialization of x |
12071207
| tst.go:3:12:3:12 | initialization of x | tst.go:4:2:4:2 | true |
12081208
| tst.go:4:2:4:2 | true | tst.go:5:7:5:7 | x |
1209-
| tst.go:4:2:4:2 | true | tst.go:12:1:12:1 | exit |
12101209
| tst.go:5:2:5:13 | skip | tst.go:12:1:12:1 | exit |
12111210
| tst.go:5:7:5:7 | x | tst.go:5:11:5:12 | 23 |
12121211
| tst.go:5:7:5:12 | ...<... | tst.go:5:7:5:12 | case ...<... |
@@ -1232,12 +1231,11 @@
12321231
| tst.go:9:12:9:12 | ...<... is false | tst.go:12:1:12:1 | exit |
12331232
| tst.go:9:12:9:12 | ...<... is true | tst.go:9:2:9:13 | skip |
12341233
| tst.go:14:1:14:1 | entry | tst.go:14:13:14:17 | argument corresponding to value |
1235-
| tst.go:14:1:21:1 | function declaration | tst.go:0:0:0:0 | exit |
1234+
| tst.go:14:1:21:1 | function declaration | tst.go:23:6:23:11 | skip |
12361235
| tst.go:14:6:14:11 | skip | tst.go:14:1:21:1 | function declaration |
12371236
| tst.go:14:13:14:17 | argument corresponding to value | tst.go:14:13:14:17 | initialization of value |
12381237
| tst.go:14:13:14:17 | initialization of value | tst.go:15:2:15:2 | true |
12391238
| tst.go:15:2:15:2 | true | tst.go:16:7:16:11 | value |
1240-
| tst.go:15:2:15:2 | true | tst.go:21:1:21:1 | exit |
12411239
| tst.go:16:2:16:34 | skip | tst.go:21:1:21:1 | exit |
12421240
| tst.go:16:7:16:11 | value | tst.go:16:15:16:33 | ...*... |
12431241
| tst.go:16:7:16:33 | ...<... | tst.go:16:7:16:33 | case ...<... |
@@ -1254,3 +1252,12 @@
12541252
| tst.go:18:15:18:38 | ...*... | tst.go:18:7:18:38 | ...<... |
12551253
| tst.go:18:38:18:38 | ...<... is false | tst.go:21:1:21:1 | exit |
12561254
| tst.go:18:38:18:38 | ...<... is true | tst.go:18:2:18:39 | skip |
1255+
| tst.go:23:1:23:1 | entry | tst.go:24:2:24:2 | true |
1256+
| tst.go:23:1:25:1 | function declaration | tst.go:27:6:27:11 | skip |
1257+
| tst.go:23:6:23:11 | skip | tst.go:23:1:25:1 | function declaration |
1258+
| tst.go:24:2:24:2 | true | tst.go:25:1:25:1 | exit |
1259+
| tst.go:27:1:27:1 | entry | tst.go:28:2:28:2 | true |
1260+
| tst.go:27:1:31:1 | function declaration | tst.go:0:0:0:0 | exit |
1261+
| tst.go:27:6:27:11 | skip | tst.go:27:1:31:1 | function declaration |
1262+
| tst.go:28:2:28:2 | true | tst.go:29:2:29:9 | skip |
1263+
| tst.go:29:2:29:9 | skip | tst.go:31:1:31:1 | exit |

ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/tst.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ func check2(value int64) {
1919

2020
}
2121
}
22+
23+
func check3() {
24+
switch { }
25+
}
26+
27+
func check4() {
28+
switch {
29+
default:
30+
}
31+
}

0 commit comments

Comments
 (0)