Skip to content
Snippets Groups Projects
Commit 7e8218ae authored by Robert Griesemer's avatar Robert Griesemer
Browse files

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

Fixes #8305.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/145680044
parent 94f3d8cf
No related branches found
No related tags found
No related merge requests found
...@@ -445,7 +445,7 @@ func (d *decodeState) array(v reflect.Value) { ...@@ -445,7 +445,7 @@ func (d *decodeState) array(v reflect.Value) {
} }
// object consumes an object from d.data[d.off-1:], decoding into the value v. // object consumes an object from d.data[d.off-1:], decoding into the value v.
// the first byte of the object ('{') has been read already. // the first byte ('{') of the object has been read already.
func (d *decodeState) object(v reflect.Value) { func (d *decodeState) object(v reflect.Value) {
// Check for unmarshaler. // Check for unmarshaler.
u, ut, pv := d.indirect(v, false) u, ut, pv := d.indirect(v, false)
...@@ -478,7 +478,9 @@ func (d *decodeState) object(v reflect.Value) { ...@@ -478,7 +478,9 @@ func (d *decodeState) object(v reflect.Value) {
t := v.Type() t := v.Type()
if t.Key().Kind() != reflect.String { if t.Key().Kind() != reflect.String {
d.saveError(&UnmarshalTypeError{"object", v.Type()}) d.saveError(&UnmarshalTypeError{"object", v.Type()})
break d.off--
d.next() // skip over { } in input
return
} }
if v.IsNil() { if v.IsNil() {
v.Set(reflect.MakeMap(t)) v.Set(reflect.MakeMap(t))
......
...@@ -406,6 +406,13 @@ var unmarshalTests = []unmarshalTest{ ...@@ -406,6 +406,13 @@ var unmarshalTests = []unmarshalTest{
ptr: new(string), ptr: new(string),
out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld", 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) { func TestMarshal(t *testing.T) {
...@@ -514,6 +521,7 @@ func TestUnmarshal(t *testing.T) { ...@@ -514,6 +521,7 @@ func TestUnmarshal(t *testing.T) {
if tt.ptr == nil { if tt.ptr == nil {
continue continue
} }
// v = new(right-type) // v = new(right-type)
v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec := NewDecoder(bytes.NewReader(in)) dec := NewDecoder(bytes.NewReader(in))
...@@ -521,7 +529,9 @@ func TestUnmarshal(t *testing.T) { ...@@ -521,7 +529,9 @@ func TestUnmarshal(t *testing.T) {
dec.UseNumber() dec.UseNumber()
} }
if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) { 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 continue
} }
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment