Skip to content

Commit 925a3f6

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

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/reflect/all_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,24 @@ func TestCopyArray(t *testing.T) {
10301030
}
10311031
}
10321032

1033+
func TestCopyArrayToSlice(t *testing.T) {
1034+
// Test copying array <=64 bits and >64bits
1035+
// See issue #4554
1036+
a8 := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
1037+
s8 := make([]byte, 8)
1038+
a9 := [9]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
1039+
s9 := make([]byte, 9)
1040+
1041+
Copy(ValueOf(s8), ValueOf(a8))
1042+
if !bytes.Equal(s8, a8[:]) {
1043+
t.Errorf("copied slice %x does not match input array %x", s8, a8[:])
1044+
}
1045+
Copy(ValueOf(s9), ValueOf(a9))
1046+
if !bytes.Equal(s9, a9[:]) {
1047+
t.Errorf("copied slice %x does not match input array %x", s9, a9[:])
1048+
}
1049+
}
1050+
10331051
func TestBigUnnamedStruct(t *testing.T) {
10341052
b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
10351053
v := ValueOf(b)

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{} {

0 commit comments

Comments
 (0)