reflect: don't construct an interface-in-interface value

v.Interaface() could construct an interface in interface value if v was
of type interface. This is not correct, and doesn't follow upstream Go
behavior. Instead, it should return the interface value itself.
This commit is contained in:
Ayke van Laethem
2021-08-15 14:58:59 +02:00
committed by Ron Evans
parent b534dd67e0
commit d15e32fb89
3 changed files with 28 additions and 0 deletions
+8
View File
@@ -67,6 +67,14 @@ func (v Value) Interface() interface{} {
// valueInterfaceUnsafe is used by the runtime to hash map keys. It should not
// be subject to the isExported check.
func valueInterfaceUnsafe(v Value) interface{} {
if v.typecode.Kind() == Interface {
// The value itself is an interface. This can happen when getting the
// value of a struct field of interface type, like this:
// type T struct {
// X interface{}
// }
return *(*interface{})(v.value)
}
if v.isIndirect() && v.typecode.Size() <= unsafe.Sizeof(uintptr(0)) {
// Value was indirect but must be put back directly in the interface
// value.