Skip to content

Commit

Permalink
fix: decode struct with pointer must be nil (goccy#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
misyuari authored Oct 19, 2023
1 parent 3d5ab28 commit df897ae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions internal/decoder/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df897ae

Please sign in to comment.