reflect: fix indirect issues with makeInt/makeUint/makeFloat

Этот коммит содержится в:
Damian Gryski 2023-03-22 10:12:42 -07:00 коммит произвёл Ayke
родитель f239e8e2d9
коммит 39f76f43fc

Просмотреть файл

@ -1235,7 +1235,18 @@ func cvtBytesString(v Value, t *rawType) Value {
func makeInt(flags valueFlags, bits uint64, t *rawType) Value { func makeInt(flags valueFlags, bits uint64, t *rawType) Value {
size := t.Size() size := t.Size()
ptr := alloc(size, nil)
v := Value{
typecode: t,
flags: flags,
}
ptr := unsafe.Pointer(&v.value)
if size > unsafe.Sizeof(uintptr(0)) {
ptr = alloc(size, nil)
v.value = ptr
}
switch size { switch size {
case 1: case 1:
*(*uint8)(ptr) = uint8(bits) *(*uint8)(ptr) = uint8(bits)
@ -1246,38 +1257,38 @@ func makeInt(flags valueFlags, bits uint64, t *rawType) Value {
case 8: case 8:
*(*uint64)(ptr) = bits *(*uint64)(ptr) = bits
} }
return Value{ return v
typecode: t,
value: ptr,
flags: flags | valueFlagIndirect,
}
} }
func makeFloat(flags valueFlags, v float64, t *rawType) Value { func makeFloat(flags valueFlags, f float64, t *rawType) Value {
size := t.Size() size := t.Size()
ptr := alloc(size, nil)
v := Value{
typecode: t,
flags: flags,
}
ptr := unsafe.Pointer(&v.value)
if size > unsafe.Sizeof(uintptr(0)) {
ptr = alloc(size, nil)
}
switch size { switch size {
case 4: case 4:
*(*float32)(ptr) = float32(v) *(*float32)(ptr) = float32(f)
case 8: case 8:
*(*float64)(ptr) = v *(*float64)(ptr) = f
}
return Value{
typecode: t,
value: ptr,
flags: flags | valueFlagIndirect,
} }
return v
} }
func makeFloat32(flags valueFlags, v float32, t *rawType) Value { func makeFloat32(flags valueFlags, f float32, t *rawType) Value {
size := t.Size() v := Value{
ptr := alloc(size, nil)
*(*float32)(ptr) = float32(v)
return Value{
typecode: t, typecode: t,
value: ptr, flags: flags,
flags: flags | valueFlagIndirect,
} }
*(*float32)(unsafe.Pointer(&v.value)) = float32(f)
return v
} }
func cvtIntString(src Value, t *rawType) Value { func cvtIntString(src Value, t *rawType) Value {