From 1480e0046f450d506fa63d04db2730de149750bd Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 2 Dec 2022 01:51:29 +0900 Subject: [PATCH] Fix checkptr error for array decoder --- decode_test.go | 10 ++++++++++ internal/decoder/array.go | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/decode_test.go b/decode_test.go index 088367e..f139eae 100644 --- a/decode_test.go +++ b/decode_test.go @@ -4005,3 +4005,13 @@ func TestIssue384(t *testing.T) { } } } + +func TestIssue408(t *testing.T) { + type T struct { + Arr [2]int32 `json:"arr"` + } + var v T + if err := json.Unmarshal([]byte(`{"arr": [1,2]}`), &v); err != nil { + t.Fatal(err) + } +} diff --git a/internal/decoder/array.go b/internal/decoder/array.go index 8ef91cf..4b23ed4 100644 --- a/internal/decoder/array.go +++ b/internal/decoder/array.go @@ -19,7 +19,9 @@ type arrayDecoder struct { } func newArrayDecoder(dec Decoder, elemType *runtime.Type, alen int, structName, fieldName string) *arrayDecoder { - zeroValue := *(*unsafe.Pointer)(unsafe_New(elemType)) + // workaround to avoid checkptr errors. cannot use `*(*unsafe.Pointer)(unsafe_New(elemType))` directly. + zeroValuePtr := unsafe_New(elemType) + zeroValue := **(**unsafe.Pointer)(unsafe.Pointer(&zeroValuePtr)) return &arrayDecoder{ valueDecoder: dec, elemType: elemType,