Skip to content

Commit 4f96a50

Browse files
ben-kriegerdgryski
authored andcommitted
reflect: fix Copy of non-pointer array with size > 64bits
1 parent e98fea0 commit 4f96a50

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/reflect/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ func buflen(v Value) (unsafe.Pointer, uintptr) {
17121712
buf = hdr.data
17131713
len = hdr.len
17141714
case Array:
1715-
if v.isIndirect() {
1715+
if v.isIndirect() || v.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
17161716
buf = v.value
17171717
} else {
17181718
buf = unsafe.Pointer(&v.value)

src/reflect/value_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/base64"
66
. "reflect"
7+
"slices"
78
"sort"
89
"strings"
910
"testing"
@@ -804,6 +805,36 @@ func TestClearMap(t *testing.T) {
804805
}
805806
}
806807

808+
func TestCopyArrayToSlice(t *testing.T) {
809+
// Test copying array <=64 bits and >64bits
810+
// See issue #4554
811+
a1 := [1]int64{1}
812+
s1 := make([]int64, 1)
813+
a2 := [2]int64{1, 2}
814+
s2 := make([]int64, 2)
815+
a8 := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
816+
s8 := make([]byte, 8)
817+
a9 := [9]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
818+
s9 := make([]byte, 9)
819+
820+
Copy(ValueOf(s1), ValueOf(a1))
821+
if !slices.Equal(s1, a1[:]) {
822+
t.Errorf("copied slice %x does not match input array %x", s1, a1[:])
823+
}
824+
Copy(ValueOf(s2), ValueOf(a2))
825+
if !slices.Equal(s2, a2[:]) {
826+
t.Errorf("copied slice %x does not match input array %x", s2, a2[:])
827+
}
828+
Copy(ValueOf(s8), ValueOf(a8))
829+
if !bytes.Equal(s8, a8[:]) {
830+
t.Errorf("copied slice %x does not match input array %x", s8, a8[:])
831+
}
832+
Copy(ValueOf(s9), ValueOf(a9))
833+
if !bytes.Equal(s9, a9[:]) {
834+
t.Errorf("copied slice %x does not match input array %x", s9, a9[:])
835+
}
836+
}
837+
807838
func TestIssue4040(t *testing.T) {
808839
var value interface{} = uint16(0)
809840

0 commit comments

Comments
 (0)