fix: decode struct with pointer must be nil (#473)

This commit is contained in:
Muhammad Noor Misyuari 2023-10-19 14:13:09 +07:00 committed by GitHub
parent 3d5ab28ab3
commit df897aec9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View file

@ -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) {

View file

@ -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