mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: Fix panic when trying to unmarshal the empty string into an integer
Fixes #3450. R=rsc, bradfitz CC=golang-dev https://golang.org/cl/6035050
This commit is contained in:
parent
61060acdc1
commit
3fab2a97e4
2 changed files with 24 additions and 0 deletions
|
|
@ -593,6 +593,11 @@ func (d *decodeState) literal(v reflect.Value) {
|
||||||
// produce more helpful error messages.
|
// produce more helpful error messages.
|
||||||
func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
|
func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
|
||||||
// Check for unmarshaler.
|
// Check for unmarshaler.
|
||||||
|
if len(item) == 0 {
|
||||||
|
//Empty string given
|
||||||
|
d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
|
||||||
|
return
|
||||||
|
}
|
||||||
wantptr := item[0] == 'n' // null
|
wantptr := item[0] == 'n' // null
|
||||||
unmarshaler, pv := d.indirect(v, wantptr)
|
unmarshaler, pv := d.indirect(v, wantptr)
|
||||||
if unmarshaler != nil {
|
if unmarshaler != nil {
|
||||||
|
|
|
||||||
|
|
@ -646,3 +646,22 @@ func TestAnonymous(t *testing.T) {
|
||||||
t.Fatal("Unmarshal: did set T.Y")
|
t.Fatal("Unmarshal: did set T.Y")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test that the empty string doesn't panic decoding when ,string is specified
|
||||||
|
// Issue 3450
|
||||||
|
func TestEmptyString(t *testing.T) {
|
||||||
|
type T2 struct {
|
||||||
|
Number1 int `json:",string"`
|
||||||
|
Number2 int `json:",string"`
|
||||||
|
}
|
||||||
|
data := `{"Number1":"1", "Number2":""}`
|
||||||
|
dec := NewDecoder(strings.NewReader(data))
|
||||||
|
var t2 T2
|
||||||
|
err := dec.Decode(&t2)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("Decode: did not return error")
|
||||||
|
}
|
||||||
|
if t2.Number1 != 1 {
|
||||||
|
t.Fatal("Decode: did not set Number1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue