Skip to content

Commit

Permalink
cmd/vet: fix panic in dead code checker on ill-formed switch statements.
Browse files Browse the repository at this point in the history
A switch statement without a tag requires case values to be bools, but
the parser does not enforce that, so AST-walking code needs to take
care.

Change-Id: I7d9abbb0324314e02a37813c2d2f6adb0d6af5e7
Reviewed-on: https://go-review.googlesource.com/107375
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
dsymonds committed Apr 15, 2018
1 parent 0b9c1ad commit 29eca06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/vet/dead.go
Expand Up @@ -45,7 +45,7 @@ func (f *File) updateDead(node ast.Node) {
}
for _, expr := range cc.List {
v := f.pkg.types[expr].Value
if v == nil || constant.BoolVal(v) {
if v == nil || v.Kind() != constant.Bool || constant.BoolVal(v) {
continue BodyLoopBool
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/vet/testdata/deadcode.go
Expand Up @@ -2123,3 +2123,12 @@ var _ = func() {
// goto without label used to panic
goto
}

func _() int {
// Empty switch tag with non-bool case value used to panic.
switch {
case 1:
println()
}
println()
}

0 comments on commit 29eca06

Please sign in to comment.