Skip to content

Commit

Permalink
cmd/compile/internal/gc: fix panic in Type Stringer
Browse files Browse the repository at this point in the history
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 golang#11614

Change-Id: I219ae7eb01edca264fad1d4a1bd261d026294b00
Reviewed-on: https://go-review.googlesource.com/14015
Reviewed-by: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
  • Loading branch information
dspezia authored and rsc committed Dec 7, 2015
1 parent 423b0cc commit 7ebb96a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/cmd/compile/internal/gc/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
26 changes: 26 additions & 0 deletions test/fixedbugs/issue11614.go
Original file line number Diff line number Diff line change
@@ -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() {
}

0 comments on commit 7ebb96a

Please sign in to comment.