From 16a358048e466a72ae05b593731f81ad81509747 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 30 Apr 2021 04:02:06 +0900 Subject: [PATCH] if elem type is slice, clear it --- decode_slice.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/decode_slice.go b/decode_slice.go index ea09f88..603d34b 100644 --- a/decode_slice.go +++ b/decode_slice.go @@ -9,6 +9,7 @@ import ( type sliceDecoder struct { elemType *rtype isElemPointerType bool + isElemSliceType bool valueDecoder decoder size uintptr arrayPool sync.Pool @@ -34,6 +35,7 @@ func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fie valueDecoder: dec, elemType: elemType, isElemPointerType: elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map, + isElemSliceType: elemType.Kind() == reflect.Slice, size: size, arrayPool: sync.Pool{ 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) if d.isElemPointerType { *(*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) if err != nil {