]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/gc: fix panic in Type Stringer
authorDidier Spezia <didier.06@gmail.com>
Sat, 29 Aug 2015 11:30:10 +0000 (11:30 +0000)
committerRuss Cox <rsc@golang.org>
Mon, 7 Dec 2015 20:18:03 +0000 (20:18 +0000)
The following code:

func n() {(interface{int})}

generates:

3: interface contains embedded non-interface int
3: type %!v(PANIC=runtime error: invalid memory address or nil pointer dereference) is not an expression

It is because the corresponding symbol (Sym field in Type object)
is nil, resulting in a panic in typefmt.

Just skip the symbol if it is nil, so that the error message becomes:

3: interface contains embedded non-interface int
3: type interface { int } is not an expression

Fixes #11614

Change-Id: I219ae7eb01edca264fad1d4a1bd261d026294b00
Reviewed-on: https://go-review.googlesource.com/14015
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>

src/cmd/compile/internal/gc/fmt.go
test/fixedbugs/issue11614.go [new file with mode: 0644]

index 2525921c8be3e1b7b3f720cab8dee616db9d8811..5f108724e08755261fd26a655678ef0d26f5bb54 100644 (file)
@@ -579,9 +579,14 @@ func typefmt(t *Type, flag int) string {
                buf.WriteString("interface {")
                for t1 := t.Type; t1 != nil; t1 = t1.Down {
                        buf.WriteString(" ")
-                       if exportname(t1.Sym.Name) {
+                       switch {
+                       case t1.Sym == nil:
+                               // Check first that a symbol is defined for this type.
+                               // Wrong interface definitions may have types lacking a symbol.
+                               break
+                       case exportname(t1.Sym.Name):
                                buf.WriteString(Sconv(t1.Sym, obj.FmtShort))
-                       } else {
+                       default:
                                buf.WriteString(Sconv(t1.Sym, obj.FmtUnsigned))
                        }
                        buf.WriteString(Tconv(t1.Type, obj.FmtShort))
diff --git a/test/fixedbugs/issue11614.go b/test/fixedbugs/issue11614.go
new file mode 100644 (file)
index 0000000..959643a
--- /dev/null
@@ -0,0 +1,26 @@
+// errorcheck
+
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that incorrect expressions involving wrong anonymous interface
+// do not generate panics in Type Stringer.
+// Does not compile.
+
+package main
+
+type I interface {
+       int // ERROR "interface contains embedded non-interface int"
+}
+
+func n() {
+       (I) // ERROR "type I is not an expression"
+}
+
+func m() {
+       (interface{int}) // ERROR "interface contains embedded non-interface int" "type interface { int } is not an expression"
+}
+
+func main() {
+}