mirror of
https://github.com/golang/go.git
synced 2025-05-23 00:11:26 +00:00
encoding/json: don't indirect pointers when decoding null
The indirect method checked the type of the child when indirecting a pointer. If the current value is a pointer and we are decoding null, we can skip this entirely and return early, avoiding the whole descent. Fixes #31776 Change-Id: Ib8b2a2357572c41f56fceac59b5a858980f3f65e Reviewed-on: https://go-review.googlesource.com/c/go/+/174699 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
parent
cdd2c265cc
commit
b6ec56bc26
@ -416,8 +416,9 @@ func (d *decodeState) valueQuoted() interface{} {
|
|||||||
|
|
||||||
// indirect walks down v allocating pointers as needed,
|
// indirect walks down v allocating pointers as needed,
|
||||||
// until it gets to a non-pointer.
|
// until it gets to a non-pointer.
|
||||||
// if it encounters an Unmarshaler, indirect stops and returns that.
|
// If it encounters an Unmarshaler, indirect stops and returns that.
|
||||||
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
|
// If decodingNull is true, indirect stops at the first settable pointer so it
|
||||||
|
// can be set to nil.
|
||||||
func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
|
func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
|
||||||
// Issue #24153 indicates that it is generally not a guaranteed property
|
// Issue #24153 indicates that it is generally not a guaranteed property
|
||||||
// that you may round-trip a reflect.Value by calling Value.Addr().Elem()
|
// that you may round-trip a reflect.Value by calling Value.Addr().Elem()
|
||||||
@ -456,7 +457,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
|
if decodingNull && v.CanSet() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -401,6 +401,11 @@ type B struct {
|
|||||||
B bool `json:",string"`
|
B bool `json:",string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DoublePtr struct {
|
||||||
|
I **int
|
||||||
|
J **int
|
||||||
|
}
|
||||||
|
|
||||||
var unmarshalTests = []unmarshalTest{
|
var unmarshalTests = []unmarshalTest{
|
||||||
// basic types
|
// basic types
|
||||||
{in: `true`, ptr: new(bool), out: true},
|
{in: `true`, ptr: new(bool), out: true},
|
||||||
@ -656,6 +661,11 @@ var unmarshalTests = []unmarshalTest{
|
|||||||
err: fmt.Errorf("json: unknown field \"X\""),
|
err: fmt.Errorf("json: unknown field \"X\""),
|
||||||
disallowUnknownFields: true,
|
disallowUnknownFields: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
in: `{"I": 0, "I": null, "J": null}`,
|
||||||
|
ptr: new(DoublePtr),
|
||||||
|
out: DoublePtr{I: nil, J: nil},
|
||||||
|
},
|
||||||
|
|
||||||
// invalid UTF-8 is coerced to valid UTF-8.
|
// invalid UTF-8 is coerced to valid UTF-8.
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user