]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: fix out of phase error unmarshaling non-string into TextUnmarshaler
authorRuss Cox <rsc@golang.org>
Tue, 14 Jul 2015 23:31:44 +0000 (19:31 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 15 Jul 2015 05:34:56 +0000 (05:34 +0000)
Fixes #9650.

Change-Id: I45b879124691e485b86c1e99a3227032283850d2
Reviewed-on: https://go-review.googlesource.com/12208
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/encoding/json/decode.go
src/encoding/json/decode_test.go

index 613641afbb44075224df4ff9dbc2a976bd67e29a..02deac4c9fe88a236287ae89eb7419c22ff51539 100644 (file)
@@ -682,6 +682,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
                        } else {
                                d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
                        }
+                       return
                }
                s, ok := unquoteBytes(item)
                if !ok {
index 4834c062ccca2b415086aef7a754d75a82164f8c..41fc9ba67326dc02e98aca08443c76692140cfee 100644 (file)
@@ -9,6 +9,7 @@ import (
        "encoding"
        "fmt"
        "image"
+       "net"
        "reflect"
        "strings"
        "testing"
@@ -1394,6 +1395,30 @@ func TestInvalidUnmarshal(t *testing.T) {
        }
 }
 
+var invalidUnmarshalTextTests = []struct {
+       v    interface{}
+       want string
+}{
+       {nil, "json: Unmarshal(nil)"},
+       {struct{}{}, "json: Unmarshal(non-pointer struct {})"},
+       {(*int)(nil), "json: Unmarshal(nil *int)"},
+       {new(net.IP), "json: cannot unmarshal string into Go value of type *net.IP"},
+}
+
+func TestInvalidUnmarshalText(t *testing.T) {
+       buf := []byte(`123`)
+       for _, tt := range invalidUnmarshalTextTests {
+               err := Unmarshal(buf, tt.v)
+               if err == nil {
+                       t.Errorf("Unmarshal expecting error, got nil")
+                       continue
+               }
+               if got := err.Error(); got != tt.want {
+                       t.Errorf("Unmarshal = %q; want %q", got, tt.want)
+               }
+       }
+}
+
 // Test that string option is ignored for invalid types.
 // Issue 9812.
 func TestInvalidStringOption(t *testing.T) {