reflect: fix Copy of non-pointer array with size > 64bits

This commit is contained in:
Ben Krieger
2024-10-25 12:10:59 -04:00
committed by Damian Gryski
parent e98fea0bba
commit 4f96a50fd0
2 changed files with 32 additions and 1 deletions
+1 -1
View File
@@ -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)
+31
View File
@@ -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)