diff options
| author | Romain Baugue <romain.baugue@gmail.com> | 2019-05-01 14:52:57 +0200 |
|---|---|---|
| committer | Daniel Martí <mvdan@mvdan.cc> | 2019-09-10 08:24:29 +0000 |
| commit | b6ec56bc26a89e9733390dd41fade5baace5a641 (patch) | |
| tree | b7d4d8684614fa23d6e55f6fad5b1ff2208b3c44 /src/encoding/json/decode.go | |
| parent | cdd2c265cc132a15e20298fbb083a70d7f3b495d (diff) | |
| download | go-b6ec56bc26a89e9733390dd41fade5baace5a641.tar.xz | |
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>
Diffstat (limited to 'src/encoding/json/decode.go')
| -rw-r--r-- | src/encoding/json/decode.go | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go index 2b135f0da5..360fc69d04 100644 --- a/src/encoding/json/decode.go +++ b/src/encoding/json/decode.go @@ -416,8 +416,9 @@ func (d *decodeState) valueQuoted() interface{} { // indirect walks down v allocating pointers as needed, // until it gets to a non-pointer. -// 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 it encounters an Unmarshaler, indirect stops and returns that. +// 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) { // Issue #24153 indicates that it is generally not a guaranteed property // 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 } - if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() { + if decodingNull && v.CanSet() { break } |
