diff options
author | LE Manh Cuong <cuong.manhle.vn@gmail.com> | 2019-04-29 22:57:25 +0700 |
---|---|---|
committer | Daniel Martí <mvdan@mvdan.cc> | 2019-04-30 04:57:42 +0000 |
commit | dcb84828a6b1d7d4d9186bc6d752cc147e255162 (patch) | |
tree | 574e4bcff325b36f0577d59bfb0873e33c42e9b9 /src/encoding/json/decode_test.go | |
parent | d021dd6a3fe61059094dba591aba265cbf6f7733 (diff) | |
download | go-git-dcb84828a6b1d7d4d9186bc6d752cc147e255162.tar.gz |
encoding/json: fix Unmarshal hang on recursive pointers
indirect walks down v until it gets to a non-pointer. But it does not
handle the case when v is a pointer to itself, like in:
var v interface{}
v = &v
Unmarshal(b, v)
So just stop immediately if we see v is a pointer to itself.
Fixes #31740
Change-Id: Ie396264119e24d70284cd9bf76dcb2050babb069
Reviewed-on: https://go-review.googlesource.com/c/go/+/174337
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding/json/decode_test.go')
-rw-r--r-- | src/encoding/json/decode_test.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go index 8da74fa3d3..719a9fa290 100644 --- a/src/encoding/json/decode_test.go +++ b/src/encoding/json/decode_test.go @@ -2334,3 +2334,15 @@ func TestUnmarshalPanic(t *testing.T) { Unmarshal([]byte("{}"), &unmarshalPanic{}) t.Fatalf("Unmarshal should have panicked") } + +// The decoder used to hang if decoding into an interface pointing to its own address. +// See golang.org/issues/31740. +func TestUnmarshalRecursivePointer(t *testing.T) { + var v interface{} + v = &v + data := []byte(`{"a": "b"}`) + + if err := Unmarshal(data, v); err != nil { + t.Fatal(err) + } +} |