]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: Fix missing error when trying to unmarshal null string into int, for...
authorEmil Hessman <c.emil.hessman@gmail.com>
Fri, 3 Jan 2014 18:13:28 +0000 (10:13 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 3 Jan 2014 18:13:28 +0000 (10:13 -0800)
Fixes #7046.

R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/47260043

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

index 4db566726e041ab84be1b08196822ac5f58f6e08..dde0d78e327380724270cab3e8b8a45ae7e1339c 100644 (file)
@@ -561,6 +561,7 @@ func (d *decodeState) object(v reflect.Value) {
                if destring {
                        d.value(reflect.ValueOf(&d.tempstr))
                        d.literalStore([]byte(d.tempstr), subv, true)
+                       d.tempstr = "" // Zero scratch space for successive values.
                } else {
                        d.value(subv)
                }
index c5a84ab832cab1c52834f9685c26db94c8da14a5..238a87fd665a88a1e21485c3054c31693a9839a6 100644 (file)
@@ -1060,6 +1060,21 @@ func TestEmptyString(t *testing.T) {
        }
 }
 
+// Test that the returned error is non-nil when trying to unmarshal null string into int, for successive ,string option
+// Issue 7046
+func TestNullString(t *testing.T) {
+       type T struct {
+               A int `json:",string"`
+               B int `json:",string"`
+       }
+       data := []byte(`{"A": "1", "B": null}`)
+       var s T
+       err := Unmarshal(data, &s)
+       if err == nil {
+               t.Fatalf("expected error; got %v", s)
+       }
+}
+
 func intp(x int) *int {
        p := new(int)
        *p = x
@@ -1110,8 +1125,8 @@ func TestInterfaceSet(t *testing.T) {
 // Issue 2540
 func TestUnmarshalNulls(t *testing.T) {
        jsonData := []byte(`{
-               "Bool"    : null, 
-               "Int"     : null, 
+               "Bool"    : null,
+               "Int"     : null,
                "Int8"    : null,
                "Int16"   : null,
                "Int32"   : null,