Skip to content

Commit

Permalink
reflect: fix Value.Index() in a specific case
Browse files Browse the repository at this point in the history
In the case where:

 - Value.Index() was called on an array
 - that array was bigger than a pointer
 - the element type fits in a pointer
 - the 'indirect' flag isn't set

the Value.Index() method would still (incorrectly) load the value.
This commit fixes that.

The next commit adds a test which would have triggered this bug so works
as a regression test.
  • Loading branch information
aykevl authored and deadprogram committed Nov 12, 2021
1 parent 823c9c2 commit 5866a47
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,17 @@ func (v Value) Index(i int) Value {
if size > unsafe.Sizeof(uintptr(0)) {
// The element fits in a pointer, but the array does not.
// Load the value from the pointer.
addr := uintptr(v.value) + elemSize*uintptr(i) // pointer to new value
addr := unsafe.Pointer(uintptr(v.value) + elemSize*uintptr(i)) // pointer to new value
value := addr
if !v.isIndirect() {
// Use a pointer to the value (don't load the value) if the
// 'indirect' flag is set.
value = unsafe.Pointer(loadValue(addr, elemSize))
}
return Value{
typecode: v.typecode.elem(),
flags: v.flags,
value: unsafe.Pointer(loadValue(unsafe.Pointer(addr), elemSize)),
value: value,
}
}

Expand Down

0 comments on commit 5866a47

Please sign in to comment.