reflect: fix off-by-one in Zero sizing
Without this, pointers wouldn't be set to nil. Add some tests.
Этот коммит содержится в:
родитель
e0aee1f23c
коммит
069c397975
2 изменённых файлов: 23 добавлений и 1 удалений
|
@ -1083,7 +1083,7 @@ func init() {
|
|||
}
|
||||
|
||||
func Zero(typ Type) Value {
|
||||
if typ.Size() < unsafe.Sizeof(uintptr(0)) {
|
||||
if typ.Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
return Value{
|
||||
typecode: typ.(*rawType),
|
||||
value: nil,
|
||||
|
|
|
@ -266,6 +266,28 @@ func TestNamedTypes(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestZero(t *testing.T) {
|
||||
s := "hello, world"
|
||||
var sptr *string = &s
|
||||
v := ValueOf(&sptr).Elem()
|
||||
v.Set(Zero(v.Type()))
|
||||
|
||||
sptr = v.Interface().(*string)
|
||||
|
||||
if sptr != nil {
|
||||
t.Errorf("failed to set a nil string pointer")
|
||||
}
|
||||
|
||||
sl := []int{1, 2, 3}
|
||||
v = ValueOf(&sl).Elem()
|
||||
v.Set(Zero(v.Type()))
|
||||
sl = v.Interface().([]int)
|
||||
|
||||
if sl != nil {
|
||||
t.Errorf("failed to set a nil slice")
|
||||
}
|
||||
}
|
||||
|
||||
func addrDecode(body interface{}) {
|
||||
vbody := ValueOf(body)
|
||||
ptr := vbody.Elem()
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче