From: Ian Lance Taylor Date: Mon, 20 Jun 2016 22:53:11 +0000 (-0700) Subject: runtime: panic with the right error on iface conversion X-Git-Tag: go1.7rc1~78 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=09834d1c082a2437b12584bebaa7353377e66f1a;p=gostls13.git runtime: panic with the right error on iface conversion A straight conversion from a type T to an interface type I, where T does not implement I, should always panic with an interface conversion error that shows the missing method. This was not happening if the conversion was done once using the comma-ok form (the result would not be OK) and then again in a straight conversion. Due to an error in the runtime package the second conversion was failing with a nil pointer dereference. Fixes #16130. Change-Id: I8b9fca0f1bb635a6181b8b76de8c2385bb7ac2d2 Reviewed-on: https://go-review.googlesource.com/24284 Run-TryBot: Ian Lance Taylor Reviewed-by: Michel Lespinasse TryBot-Result: Gobot Gobot Reviewed-by: Andrew Gerrand Reviewed-by: Keith Randall --- diff --git a/src/runtime/iface.go b/src/runtime/iface.go index b57d1cc63c..1690147fac 100644 --- a/src/runtime/iface.go +++ b/src/runtime/iface.go @@ -54,7 +54,6 @@ func getitab(inter *interfacetype, typ *_type, canfail bool) *itab { for m = (*itab)(atomic.Loadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link { if m.inter == inter && m._type == typ { if m.bad != 0 { - m = nil if !canfail { // this can only happen if the conversion // was already done once using the , ok form @@ -64,6 +63,7 @@ func getitab(inter *interfacetype, typ *_type, canfail bool) *itab { // adding the itab again, which will throw an error. additab(m, locked != 0, false) } + m = nil } if locked != 0 { unlock(&ifaceLock) diff --git a/test/fixedbugs/issue16130.go b/test/fixedbugs/issue16130.go new file mode 100644 index 0000000000..19c8264c6f --- /dev/null +++ b/test/fixedbugs/issue16130.go @@ -0,0 +1,43 @@ +// run + +// Copyright 2016 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 an interface conversion error panics with an "interface +// conversion" run-time error. It was (incorrectly) panicing with a +// "nil pointer dereference." + +package main + +import ( + "fmt" + "runtime" + "strings" +) + +type I interface { + Get() int +} + +func main() { + defer func() { + r := recover() + if r == nil { + panic("expected panic") + } + re, ok := r.(runtime.Error) + if !ok { + panic(fmt.Sprintf("got %T, expected runtime.Error", r)) + } + if !strings.Contains(re.Error(), "interface conversion") { + panic(fmt.Sprintf("got %q, expected interface conversion error", re.Error())) + } + }() + e := (interface{})(0) + if _, ok := e.(I); ok { + panic("unexpected interface conversion success") + } + fmt.Println(e.(I)) + panic("unexpected interface conversion success") +}