Fix pointer alignment error

This commit is contained in:
Masaaki Goshima 2021-03-29 02:44:49 +09:00
parent e38c3606b3
commit e660ae468d
1 changed files with 17 additions and 11 deletions

View File

@ -7,12 +7,13 @@ import (
) )
type sliceDecoder struct { type sliceDecoder struct {
elemType *rtype elemType *rtype
valueDecoder decoder isElemPointerType bool
size uintptr valueDecoder decoder
arrayPool sync.Pool size uintptr
structName string arrayPool sync.Pool
fieldName string structName string
fieldName string
} }
// If use reflect.SliceHeader, data type is uintptr. // If use reflect.SliceHeader, data type is uintptr.
@ -30,9 +31,10 @@ const (
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fieldName string) *sliceDecoder { func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fieldName string) *sliceDecoder {
return &sliceDecoder{ return &sliceDecoder{
valueDecoder: dec, valueDecoder: dec,
elemType: elemType, elemType: elemType,
size: size, isElemPointerType: elemType.Kind() == reflect.Ptr,
size: size,
arrayPool: sync.Pool{ arrayPool: sync.Pool{
New: func() interface{} { New: func() interface{} {
return &sliceHeader{ return &sliceHeader{
@ -115,7 +117,9 @@ func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) er
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size) ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer if d.isElemPointerType {
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer
}
if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil { if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil {
return err return err
} }
@ -227,7 +231,9 @@ func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer)
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size) ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer if d.isElemPointerType {
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer
}
c, err := d.valueDecoder.decode(buf, cursor, depth, ep) c, err := d.valueDecoder.decode(buf, cursor, depth, ep)
if err != nil { if err != nil {
return 0, err return 0, err