Skip to content

Commit

Permalink
reflect: fix Copy of non-pointer array with size > 64bits
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-krieger committed Nov 4, 2024
1 parent d00abd5 commit a80c83b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ func buflen(v Value) (unsafe.Pointer, uintptr) {
buf = hdr.data
len = hdr.len
case Array:
if v.isIndirect() {
if v.isIndirect() || v.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
buf = v.value
} else {
buf = unsafe.Pointer(&v.value)
Expand Down
31 changes: 31 additions & 0 deletions src/reflect/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
. "reflect"
"slices"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -804,6 +805,36 @@ func TestClearMap(t *testing.T) {
}
}

func TestCopyArrayToSlice(t *testing.T) {
// Test copying array <=64 bits and >64bits
// See issue #4554
a1 := [1]int64{1}
s1 := make([]int64, 1)
a2 := [2]int64{1, 2}
s2 := make([]int64, 2)
a8 := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
s8 := make([]byte, 8)
a9 := [9]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
s9 := make([]byte, 9)

Copy(ValueOf(s1), ValueOf(a1))
if !slices.Equal(s1, a1[:]) {
t.Errorf("copied slice %x does not match input array %x", s1, a1[:])
}
Copy(ValueOf(s2), ValueOf(a2))
if !slices.Equal(s2, a2[:]) {
t.Errorf("copied slice %x does not match input array %x", s2, a2[:])
}
Copy(ValueOf(s8), ValueOf(a8))
if !bytes.Equal(s8, a8[:]) {
t.Errorf("copied slice %x does not match input array %x", s8, a8[:])
}
Copy(ValueOf(s9), ValueOf(a9))
if !bytes.Equal(s9, a9[:]) {
t.Errorf("copied slice %x does not match input array %x", s9, a9[:])
}
}

func TestIssue4040(t *testing.T) {
var value interface{} = uint16(0)

Expand Down

0 comments on commit a80c83b

Please sign in to comment.