From bf35de8f918d8a3711e25e93638e5726a21a1446 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 13 Aug 2021 15:49:53 +0900 Subject: [PATCH] Fix assign nil slice value --- internal/decoder/slice.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/decoder/slice.go b/internal/decoder/slice.go index 853a555..85b6e11 100644 --- a/internal/decoder/slice.go +++ b/internal/decoder/slice.go @@ -9,6 +9,13 @@ import ( "github.com/goccy/go-json/internal/runtime" ) +var ( + sliceType = runtime.Type2RType( + reflect.TypeOf((*sliceHeader)(nil)).Elem(), + ) + nilSlice = unsafe.Pointer(&sliceHeader{}) +) + type sliceDecoder struct { elemType *runtime.Type isElemPointerType bool @@ -107,7 +114,7 @@ func (d *sliceDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) er if err := nullBytes(s); err != nil { return err } - *(*unsafe.Pointer)(p) = nil + typedmemmove(sliceType, p, nilSlice) return nil case '[': s.cursor++ @@ -216,7 +223,7 @@ func (d *sliceDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe return 0, err } cursor += 4 - *(*unsafe.Pointer)(p) = nil + typedmemmove(sliceType, p, nilSlice) return cursor, nil case '[': cursor++