Skip to content

Commit 05e2f5d

Browse files
author
Ben Krieger
committed
reflect: fix Copy of non-pointer array with size > 64bits
1 parent 191f2e0 commit 05e2f5d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/reflect/value.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ func decomposeInterface(i interface{}) (unsafe.Pointer, unsafe.Pointer)
7272

7373
func ValueOf(i interface{}) Value {
7474
typecode, value := decomposeInterface(i)
75-
return Value{
75+
v := Value{
7676
typecode: (*rawType)(typecode),
7777
value: value,
7878
flags: valueFlagExported,
7979
}
80+
if v.typecode.Kind() == Array && v.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
81+
v.flags |= valueFlagIndirect
82+
}
83+
return v
8084
}
8185

8286
func (v Value) Interface() interface{} {

src/reflect/value_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,24 @@ func TestClearMap(t *testing.T) {
791791
}
792792
}
793793

794+
func TestCopyArrayToSlice(t *testing.T) {
795+
// Test copying array <=64 bits and >64bits
796+
// See issue #4554
797+
a8 := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
798+
s8 := make([]byte, 8)
799+
a9 := [9]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
800+
s9 := make([]byte, 9)
801+
802+
Copy(ValueOf(s8), ValueOf(a8))
803+
if !bytes.Equal(s8, a8[:]) {
804+
t.Errorf("copied slice %x does not match input array %x", s8, a8[:])
805+
}
806+
Copy(ValueOf(s9), ValueOf(a9))
807+
if !bytes.Equal(s9, a9[:]) {
808+
t.Errorf("copied slice %x does not match input array %x", s9, a9[:])
809+
}
810+
}
811+
794812
func TestIssue4040(t *testing.T) {
795813
var value interface{} = uint16(0)
796814

0 commit comments

Comments
 (0)