From a80c83b7c2d9d1e842dd6a86b1e8a748e9e6babd Mon Sep 17 00:00:00 2001 From: Ben Krieger Date: Fri, 25 Oct 2024 12:10:59 -0400 Subject: [PATCH] reflect: fix Copy of non-pointer array with size > 64bits --- src/reflect/value.go | 2 +- src/reflect/value_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/reflect/value.go b/src/reflect/value.go index 78453be343..69b57fd1cc 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -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) diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go index a32ba4fa9d..4df9db4d19 100644 --- a/src/reflect/value_test.go +++ b/src/reflect/value_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" . "reflect" + "slices" "sort" "strings" "testing" @@ -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)