]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: fix panic in dead code checker on ill-formed switch statements.
authorDavid Symonds <dsymonds@golang.org>
Sun, 15 Apr 2018 23:41:05 +0000 (09:41 +1000)
committerDavid Symonds <dsymonds@golang.org>
Sun, 15 Apr 2018 23:51:20 +0000 (23:51 +0000)
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>
src/cmd/vet/dead.go
src/cmd/vet/testdata/deadcode.go

index 130f619626db53c3d51ed8460c852238721e39d4..0facec552583a028c7a5729c8ad6ea78a9c4f2c4 100644 (file)
@@ -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
                                        }
                                }
index 5370bc32f6577e37222d0ee10ac7ac3d4a0b657d..d1a7adee38d2c9c30dc93f9e67bbd2b2049e9693 100644 (file)
@@ -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()
+}