From e660ae468de69b0649edb02860137b3a0093fd3d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 29 Mar 2021 02:44:49 +0900 Subject: [PATCH] Fix pointer alignment error --- decode_slice.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/decode_slice.go b/decode_slice.go index e70588e..d9a6749 100644 --- a/decode_slice.go +++ b/decode_slice.go @@ -7,12 +7,13 @@ import ( ) type sliceDecoder struct { - elemType *rtype - valueDecoder decoder - size uintptr - arrayPool sync.Pool - structName string - fieldName string + elemType *rtype + isElemPointerType bool + valueDecoder decoder + size uintptr + arrayPool sync.Pool + structName string + fieldName string } // 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 { return &sliceDecoder{ - valueDecoder: dec, - elemType: elemType, - size: size, + valueDecoder: dec, + elemType: elemType, + isElemPointerType: elemType.Kind() == reflect.Ptr, + size: size, arrayPool: sync.Pool{ New: func() interface{} { return &sliceHeader{ @@ -115,7 +117,9 @@ func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) er copySlice(d.elemType, dst, src) } 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 { return err } @@ -227,7 +231,9 @@ func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) copySlice(d.elemType, dst, src) } 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) if err != nil { return 0, err