Commit 29eca06f authored by David Symonds's avatar David Symonds

cmd/vet: fix panic in dead code checker on ill-formed switch statements.

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/107375Reviewed-by: default avatarRob Pike <r@golang.org>
parent 0b9c1ad2
...@@ -45,7 +45,7 @@ func (f *File) updateDead(node ast.Node) { ...@@ -45,7 +45,7 @@ func (f *File) updateDead(node ast.Node) {
} }
for _, expr := range cc.List { for _, expr := range cc.List {
v := f.pkg.types[expr].Value v := f.pkg.types[expr].Value
if v == nil || constant.BoolVal(v) { if v == nil || v.Kind() != constant.Bool || constant.BoolVal(v) {
continue BodyLoopBool continue BodyLoopBool
} }
} }
......
...@@ -2123,3 +2123,12 @@ var _ = func() { ...@@ -2123,3 +2123,12 @@ var _ = func() {
// goto without label used to panic // goto without label used to panic
goto goto
} }
func _() int {
// Empty switch tag with non-bool case value used to panic.
switch {
case 1:
println()
}
println()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment