Skip to content

Commit 3666d9f

Browse files
Romain Bauguemvdan
Romain Baugue
authored andcommitted
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í <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 166be9c commit 3666d9f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

decode.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,9 @@ func (d *decodeState) valueQuoted() interface{} {
416416

417417
// indirect walks down v allocating pointers as needed,
418418
// until it gets to a non-pointer.
419-
// if it encounters an Unmarshaler, indirect stops and returns that.
420-
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
419+
// If it encounters an Unmarshaler, indirect stops and returns that.
420+
// If decodingNull is true, indirect stops at the first settable pointer so it
421+
// can be set to nil.
421422
func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
422423
// Issue #24153 indicates that it is generally not a guaranteed property
423424
// 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
456457
break
457458
}
458459

459-
if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
460+
if decodingNull && v.CanSet() {
460461
break
461462
}
462463

decode_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ type B struct {
401401
B bool `json:",string"`
402402
}
403403

404+
type DoublePtr struct {
405+
I **int
406+
J **int
407+
}
408+
404409
var unmarshalTests = []unmarshalTest{
405410
// basic types
406411
{in: `true`, ptr: new(bool), out: true},
@@ -656,6 +661,11 @@ var unmarshalTests = []unmarshalTest{
656661
err: fmt.Errorf("json: unknown field \"X\""),
657662
disallowUnknownFields: true,
658663
},
664+
{
665+
in: `{"I": 0, "I": null, "J": null}`,
666+
ptr: new(DoublePtr),
667+
out: DoublePtr{I: nil, J: nil},
668+
},
659669

660670
// invalid UTF-8 is coerced to valid UTF-8.
661671
{

0 commit comments

Comments
 (0)