Skip to content

Commit 97c6c38

Browse files
author
Ben Krieger
committed
reflect: fix Copy of non-pointer array with size > 64bits
1 parent 93bfdc5 commit 97c6c38

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/reflect/value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ func convertOp(src Value, typ Type) (Value, bool) {
13411341
return Value{
13421342
typecode: rtype,
13431343
value: (*sliceHeader)(src.value).data,
1344-
flags: src.flags,
1344+
flags: src.flags | valueFlagIndirect | valueFlagRO,
13451345
}, true
13461346
}
13471347
case Pointer:
@@ -1715,7 +1715,7 @@ func buflen(v Value) (unsafe.Pointer, uintptr) {
17151715
buf = hdr.data
17161716
len = hdr.len
17171717
case Array:
1718-
if v.isIndirect() {
1718+
if v.isIndirect() || v.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
17191719
buf = v.value
17201720
} else {
17211721
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"
@@ -791,6 +792,36 @@ func TestClearMap(t *testing.T) {
791792
}
792793
}
793794

795+
func TestCopyArrayToSlice(t *testing.T) {
796+
// Test copying array <=64 bits and >64bits
797+
// See issue #4554
798+
a1 := [1]int64{1}
799+
s1 := make([]int64, 1)
800+
a2 := [2]int64{1, 2}
801+
s2 := make([]int64, 2)
802+
a8 := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
803+
s8 := make([]byte, 8)
804+
a9 := [9]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
805+
s9 := make([]byte, 9)
806+
807+
Copy(ValueOf(s1), ValueOf(a1))
808+
if !slices.Equal(s1, a1[:]) {
809+
t.Errorf("copied slice %x does not match input array %x", s1, a1[:])
810+
}
811+
Copy(ValueOf(s2), ValueOf(a2))
812+
if !slices.Equal(s2, a2[:]) {
813+
t.Errorf("copied slice %x does not match input array %x", s2, a2[:])
814+
}
815+
Copy(ValueOf(s8), ValueOf(a8))
816+
if !bytes.Equal(s8, a8[:]) {
817+
t.Errorf("copied slice %x does not match input array %x", s8, a8[:])
818+
}
819+
Copy(ValueOf(s9), ValueOf(a9))
820+
if !bytes.Equal(s9, a9[:]) {
821+
t.Errorf("copied slice %x does not match input array %x", s9, a9[:])
822+
}
823+
}
824+
794825
func TestIssue4040(t *testing.T) {
795826
var value interface{} = uint16(0)
796827

0 commit comments

Comments
 (0)