Skip to content

Commit 671bddf

Browse files
committed
encoding/json: fix out of phase error unmarshaling non-string into TextUnmarshaler
Fixes #9650. Change-Id: I45b879124691e485b86c1e99a3227032283850d2 Reviewed-on: https://go-review.googlesource.com/12208 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent 29f03a3 commit 671bddf

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/encoding/json/decode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
682682
} else {
683683
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
684684
}
685+
return
685686
}
686687
s, ok := unquoteBytes(item)
687688
if !ok {

src/encoding/json/decode_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding"
1010
"fmt"
1111
"image"
12+
"net"
1213
"reflect"
1314
"strings"
1415
"testing"
@@ -1394,6 +1395,30 @@ func TestInvalidUnmarshal(t *testing.T) {
13941395
}
13951396
}
13961397

1398+
var invalidUnmarshalTextTests = []struct {
1399+
v interface{}
1400+
want string
1401+
}{
1402+
{nil, "json: Unmarshal(nil)"},
1403+
{struct{}{}, "json: Unmarshal(non-pointer struct {})"},
1404+
{(*int)(nil), "json: Unmarshal(nil *int)"},
1405+
{new(net.IP), "json: cannot unmarshal string into Go value of type *net.IP"},
1406+
}
1407+
1408+
func TestInvalidUnmarshalText(t *testing.T) {
1409+
buf := []byte(`123`)
1410+
for _, tt := range invalidUnmarshalTextTests {
1411+
err := Unmarshal(buf, tt.v)
1412+
if err == nil {
1413+
t.Errorf("Unmarshal expecting error, got nil")
1414+
continue
1415+
}
1416+
if got := err.Error(); got != tt.want {
1417+
t.Errorf("Unmarshal = %q; want %q", got, tt.want)
1418+
}
1419+
}
1420+
}
1421+
13971422
// Test that string option is ignored for invalid types.
13981423
// Issue 9812.
13991424
func TestInvalidStringOption(t *testing.T) {

0 commit comments

Comments
 (0)