diff options
| author | Russ Cox <rsc@golang.org> | 2012-06-07 01:48:55 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2012-06-07 01:48:55 -0400 |
| commit | 09b736a2ab56ee520e3f5909c09c8417fe61db26 (patch) | |
| tree | 3b92a4773a8a6237b25b5ad75cd61f06dd9d4158 /src/pkg/encoding/json/decode_test.go | |
| parent | 8022a1a58836c5d5eb3ef4f78bdb701bac56fe93 (diff) | |
| download | go-09b736a2ab56ee520e3f5909c09c8417fe61db26.tar.xz | |
encoding/json: fix panic unmarshaling into non-nil interface value
Fixes #3614.
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6306051
Diffstat (limited to 'src/pkg/encoding/json/decode_test.go')
| -rw-r--r-- | src/pkg/encoding/json/decode_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go index c7dce53f29..5a85e3f751 100644 --- a/src/pkg/encoding/json/decode_test.go +++ b/src/pkg/encoding/json/decode_test.go @@ -683,3 +683,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) + } + } +} |
