encoding/json: fix panics on type mismatches.

Fixes #4222.
Fixes #4628.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/7100049
This commit is contained in:
Rémy Oudompheng 2013-01-14 08:44:16 +01:00
parent 06af0ea3f3
commit 406ca3c2f1
2 changed files with 46 additions and 10 deletions

View file

@ -1042,3 +1042,25 @@ func TestStringKind(t *testing.T) {
}
}
var decodeTypeErrorTests = []struct {
dest interface{}
src string
}{
{new(string), `{"user": "name"}`}, // issue 4628.
{new(error), `{}`}, // issue 4222
{new(error), `[]`},
{new(error), `""`},
{new(error), `123`},
{new(error), `true`},
}
func TestUnmarshalTypeError(t *testing.T) {
for _, item := range decodeTypeErrorTests {
err := Unmarshal([]byte(item.src), item.dest)
if _, ok := err.(*UnmarshalTypeError); !ok {
t.Errorf("expected type error for Unmarshal(%q, type %T): got %v instead",
item.src, item.dest, err)
}
}
}