if elem type is slice, clear it

This commit is contained in:
Masaaki Goshima 2021-04-30 04:02:06 +09:00
parent dc2d025d2a
commit 16a358048e
1 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
type sliceDecoder struct { type sliceDecoder struct {
elemType *rtype elemType *rtype
isElemPointerType bool isElemPointerType bool
isElemSliceType bool
valueDecoder decoder valueDecoder decoder
size uintptr size uintptr
arrayPool sync.Pool arrayPool sync.Pool
@ -34,6 +35,7 @@ func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fie
valueDecoder: dec, valueDecoder: dec,
elemType: elemType, elemType: elemType,
isElemPointerType: elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map, isElemPointerType: elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map,
isElemSliceType: elemType.Kind() == reflect.Slice,
size: size, size: size,
arrayPool: sync.Pool{ arrayPool: sync.Pool{
New: func() interface{} { New: func() interface{} {
@ -233,6 +235,12 @@ func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer)
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size) ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
if d.isElemPointerType { if d.isElemPointerType {
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer *(*unsafe.Pointer)(ep) = nil // initialize elem pointer
} else if d.isElemSliceType {
*(*sliceHeader)(ep) = sliceHeader{
data: newArray(d.elemType, 0),
len: 0,
cap: 0,
}
} }
c, err := d.valueDecoder.decode(buf, cursor, depth, ep) c, err := d.valueDecoder.decode(buf, cursor, depth, ep)
if err != nil { if err != nil {