aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2012-06-13 16:24:48 -0400
committerRuss Cox <rsc@golang.org>2012-06-13 16:24:48 -0400
commitdc0f7d6fde051ce00337af31351120b203825863 (patch)
treef02e8bc226c1b950f2c89cfdcc0f39963b3e7d23
parentcec2715018b9adbc518eccbf7022b48bc2e3403f (diff)
downloadgo-dc0f7d6fde051ce00337af31351120b203825863.tar.gz
go-dc0f7d6fde051ce00337af31351120b203825863.zip
[release-branch.go1] encoding/json: fix panic unmarshaling into non-nil interface value
««« backport bee83c1509a3 encoding/json: fix panic unmarshaling into non-nil interface value Fixes #3614. R=golang-dev, adg CC=golang-dev https://golang.org/cl/6306051 »»»
-rw-r--r--src/pkg/encoding/json/decode.go9
-rw-r--r--src/pkg/encoding/json/decode_test.go46
2 files changed, 53 insertions, 2 deletions
diff --git a/src/pkg/encoding/json/decode.go b/src/pkg/encoding/json/decode.go
index dcfdeec3c5..d61f887064 100644
--- a/src/pkg/encoding/json/decode.go
+++ b/src/pkg/encoding/json/decode.go
@@ -273,9 +273,14 @@ func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler,
_, isUnmarshaler = v.Interface().(Unmarshaler)
}
+ // Load value from interface, but only if the result will be
+ // usefully addressable.
if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() {
- v = iv.Elem()
- continue
+ e := iv.Elem()
+ if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
+ v = e
+ continue
+ }
}
pv := v
diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go
index 2a7549cb68..6fac22c4a3 100644
--- a/src/pkg/encoding/json/decode_test.go
+++ b/src/pkg/encoding/json/decode_test.go
@@ -657,3 +657,49 @@ func TestEmptyString(t *testing.T) {
t.Fatal("Decode: did not set Number1")
}
}
+
+func intp(x int) *int {
+ p := new(int)
+ *p = x
+ return p
+}
+
+func intpp(x *int) **int {
+ pp := new(*int)
+ *pp = x
+ return pp
+}
+
+var interfaceSetTests = []struct {
+ pre interface{}
+ json string
+ post interface{}
+}{
+ {"foo", `"bar"`, "bar"},
+ {"foo", `2`, 2.0},
+ {"foo", `true`, true},
+ {"foo", `null`, nil},
+
+ {nil, `null`, nil},
+ {new(int), `null`, nil},
+ {(*int)(nil), `null`, nil},
+ {new(*int), `null`, new(*int)},
+ {(**int)(nil), `null`, nil},
+ {intp(1), `null`, nil},
+ {intpp(nil), `null`, intpp(nil)},
+ {intpp(intp(1)), `null`, intpp(nil)},
+}
+
+func TestInterfaceSet(t *testing.T) {
+ for _, tt := range interfaceSetTests {
+ b := struct{ X interface{} }{tt.pre}
+ blob := `{"X":` + tt.json + `}`
+ if err := Unmarshal([]byte(blob), &b); err != nil {
+ t.Errorf("Unmarshal %#q: %v", blob, err)
+ continue
+ }
+ if !reflect.DeepEqual(b.X, tt.post) {
+ t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post)
+ }
+ }
+}