]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1] encoding/json: Fix panic when trying to unmarshal the empty...
authorMichael Chaten <mchaten@gmail.com>
Wed, 13 Jun 2012 20:23:41 +0000 (16:23 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 13 Jun 2012 20:23:41 +0000 (16:23 -0400)
««« backport 7ee60b35f644
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

»»»

src/pkg/encoding/json/decode.go
src/pkg/encoding/json/decode_test.go

index 110c6fd62386e5af5ad5871c9b31067c4175c569..dcfdeec3c5a5a07c833d40213c90ae3669bdca8a 100644 (file)
@@ -588,6 +588,11 @@ func (d *decodeState) literal(v reflect.Value) {
 // produce more helpful error messages.
 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
        // 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
        unmarshaler, pv := d.indirect(v, wantptr)
        if unmarshaler != nil {
index d758758d97816352b6c50bc2451cb2cea3d6d510..2a7549cb689a3ebb76af1544aa4701267994070b 100644 (file)
@@ -638,3 +638,22 @@ func TestAnonymous(t *testing.T) {
                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")
+       }
+}