Skip to content

Commit

Permalink
encoding/json: fix unmarshal invalid json.Number
Browse files Browse the repository at this point in the history
Unmarshal of invalid values, that is a value that is not a number,
into fields of type json.Number should fail.

Fixes #14702
  • Loading branch information
breml committed Sep 12, 2019
1 parent 6c6ad30 commit c34066e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,9 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
v.SetBytes(b[:n])
case reflect.String:
v.SetString(string(s))
if v.Type() == numberType && !isValidNumber(string(s)) {
return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)
}
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(string(s)))
Expand Down
31 changes: 31 additions & 0 deletions src/encoding/json/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,37 @@ func TestNumberAccessors(t *testing.T) {
}
}

// golang.org/issue/14702
func TestUnmarshalNumber(t *testing.T) {
var b Number
if err := Unmarshal([]byte(`invalid`), &b); err == nil {
t.Error("Unmarshal of invalid value into json.Number did not fail.")
}
if err := Unmarshal([]byte(`"invalid"`), &b); err == nil {
t.Error("Unmarshal of invalid value into json.Number did not fail.")
}

validJson := `{"A":"invalid"}`

var test struct {
A Number
}
if err := Unmarshal([]byte(validJson), &test); err == nil {
t.Error("Unmarshal of invalid value into struct field of type json.Number did not fail.")
}
var testString struct {
A Number `json:",string"`
}
if err := Unmarshal([]byte(validJson), &testString); err == nil {
t.Error("Unmarshal of invalid value into struct field of type json.Number with string option did not fail.")
}

var testMap map[string]Number
if err := Unmarshal([]byte(validJson), &testMap); err == nil {
t.Error("Unmarshal of invalid value into map[string]json.Number did not fail.")
}
}

func TestLargeByteSlice(t *testing.T) {
s0 := make([]byte, 2000)
for i := range s0 {
Expand Down

0 comments on commit c34066e

Please sign in to comment.