Skip to content

Commit 0818a12

Browse files
aykevldeadprogram
authored andcommitted
reflect: fix IsNil and Pointer for addressable pointer types
1 parent 7c758b0 commit 0818a12

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/reflect/value.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ func (v Value) Kind() Kind {
6969
return v.Type().Kind()
7070
}
7171

72+
// IsNil returns whether the value is the nil value. It panics if the value Kind
73+
// is not a channel, map, pointer, function, slice, or interface.
7274
func (v Value) IsNil() bool {
7375
switch v.Kind() {
7476
case Chan, Map, Ptr:
77+
if v.isIndirect() {
78+
return *(*uintptr)(v.value) == 0
79+
}
7580
return v.value == nil
7681
case Func:
7782
if v.value == nil {
@@ -96,9 +101,14 @@ func (v Value) IsNil() bool {
96101
}
97102
}
98103

104+
// Pointer returns the underlying pointer of the given value for the following
105+
// types: chan, map, pointer, unsafe.Pointer, slice, func.
99106
func (v Value) Pointer() uintptr {
100107
switch v.Kind() {
101108
case Chan, Map, Ptr, UnsafePointer:
109+
if v.isIndirect() {
110+
return *(*uintptr)(v.value)
111+
}
102112
return uintptr(v.value)
103113
case Slice:
104114
slice := (*SliceHeader)(v.value)

0 commit comments

Comments
 (0)