From 29eca06ff078b2ebb60791988f83468d72ed44ef Mon Sep 17 00:00:00 2001 From: David Symonds Date: Mon, 16 Apr 2018 09:41:05 +1000 Subject: [PATCH] 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/107375 Reviewed-by: Rob Pike --- src/cmd/vet/dead.go | 2 +- src/cmd/vet/testdata/deadcode.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/vet/dead.go b/src/cmd/vet/dead.go index 130f619626..0facec5525 100644 --- a/src/cmd/vet/dead.go +++ b/src/cmd/vet/dead.go @@ -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 } } diff --git a/src/cmd/vet/testdata/deadcode.go b/src/cmd/vet/testdata/deadcode.go index 5370bc32f6..d1a7adee38 100644 --- a/src/cmd/vet/testdata/deadcode.go +++ b/src/cmd/vet/testdata/deadcode.go @@ -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() +}