diff --git a/decode_test.go b/decode_test.go index df914eda..8f32cedb 100644 --- a/decode_test.go +++ b/decode_test.go @@ -197,6 +197,13 @@ func Test_Decoder(t *testing.T) { assertEq(t, "interface{}", v.F, nil) assertEq(t, "nilfunc", true, v.G == nil) }) + t.Run("struct.pointer must be nil", func(t *testing.T) { + var v struct { + A *int + } + json.Unmarshal([]byte(`{"a": "alpha"}`), &v) + assertEq(t, "struct.A", v.A, (*int)(nil)) + }) }) t.Run("interface", func(t *testing.T) { t.Run("number", func(t *testing.T) { diff --git a/internal/decoder/ptr.go b/internal/decoder/ptr.go index de12e105..ae229946 100644 --- a/internal/decoder/ptr.go +++ b/internal/decoder/ptr.go @@ -85,6 +85,7 @@ func (d *ptrDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.P } c, err := d.dec.Decode(ctx, cursor, depth, newptr) if err != nil { + *(*unsafe.Pointer)(p) = nil return 0, err } cursor = c