aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBen Krieger <[email protected]>2024-10-25 12:10:59 -0400
committerDamian Gryski <[email protected]>2024-11-13 10:28:33 -0700
commit4f96a50fd0ba05f186403f8bcbf8e8871dcc2534 (patch)
tree5f575ca4c6d0e298c926956443884f37fee847a4
parente98fea0bba058b0a46dd1874d9a9f516bcc19304 (diff)
downloadtinygo-4f96a50fd0ba05f186403f8bcbf8e8871dcc2534.tar.gz
tinygo-4f96a50fd0ba05f186403f8bcbf8e8871dcc2534.zip
reflect: fix Copy of non-pointer array with size > 64bits
-rw-r--r--src/reflect/value.go2
-rw-r--r--src/reflect/value_test.go31
2 files changed, 32 insertions, 1 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index 78453be34..69b57fd1c 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 a32ba4fa9..4df9db4d1 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)