encoding/json: don't panic on incorrect map argument

Fixes #8305.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/145680044
This commit is contained in:
Robert Griesemer 2014-10-01 16:24:17 -07:00
parent 94f3d8cfed
commit 7e8218aedd
2 changed files with 15 additions and 3 deletions

View file

@ -406,6 +406,13 @@ var unmarshalTests = []unmarshalTest{
ptr: new(string),
out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld",
},
// issue 8305
{
in: `{"2009-11-10T23:00:00Z": "hello world"}`,
ptr: &map[time.Time]string{},
err: &UnmarshalTypeError{"object", reflect.TypeOf(map[time.Time]string{})},
},
}
func TestMarshal(t *testing.T) {
@ -514,6 +521,7 @@ func TestUnmarshal(t *testing.T) {
if tt.ptr == nil {
continue
}
// v = new(right-type)
v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec := NewDecoder(bytes.NewReader(in))
@ -521,7 +529,9 @@ func TestUnmarshal(t *testing.T) {
dec.UseNumber()
}
if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: %v want %v", i, err, tt.err)
t.Errorf("#%d: %v, want %v", i, err, tt.err)
continue
} else if err != nil {
continue
}
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {