aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/json')
-rw-r--r--src/encoding/json/decode.go8
-rw-r--r--src/encoding/json/decode_test.go12
2 files changed, 20 insertions, 0 deletions
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go
index 3ca3d7803e..bdd94e34ce 100644
--- a/src/encoding/json/decode.go
+++ b/src/encoding/json/decode.go
@@ -535,6 +535,14 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
break
}
+
+ // Prevent infinite loop if v is an interface pointing to its own address:
+ // var v interface{}
+ // v = &v
+ if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
+ v = v.Elem()
+ break
+ }
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
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)
+ }
+}