Skip to content

Commit

Permalink
encoding/json: fix scanner byte offset on scanEnd
Browse files Browse the repository at this point in the history
scanEnd is delayed one byte so we decrement
the scanner bytes count by 1 to ensure that
this value is correct in the next call of Decode.
  • Loading branch information
itchyny committed Sep 2, 2019
1 parent fa7d40a commit 0ac25d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/encoding/json/decode_test.go
Expand Up @@ -2345,6 +2345,41 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) {
}
}

func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
tests := []struct {
in string
err error
}{{
in: `1 false null :`,
err: &SyntaxError{"invalid character ':' looking for beginning of value", 14},
}, {
in: `1 [] [,]`,
err: &SyntaxError{"invalid character ',' looking for beginning of value", 7},
}, {
in: `1 [] [true:]`,
err: &SyntaxError{"invalid character ':' after array element", 11},
}, {
in: `1 {} {"x"=}`,
err: &SyntaxError{"invalid character '=' after object key", 14},
}, {
in: `falsetruenul#`,
err: &SyntaxError{"invalid character '#' in literal null (expecting 'l')", 13},
}}
for i, tt := range tests {
dec := NewDecoder(strings.NewReader(tt.in))
var err error
for {
var v interface{}
if err = dec.Decode(&v); err != nil {
break
}
}
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: got %#v, want %#v", i, err, tt.err)
}
}
}

type unmarshalPanic struct{}

func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) }
Expand Down
4 changes: 4 additions & 0 deletions src/encoding/json/stream.go
Expand Up @@ -102,6 +102,10 @@ Input:
dec.scan.bytes++
switch dec.scan.step(&dec.scan, c) {
case scanEnd:
// scanEnd is delayed one byte so we decrement
// the scanner bytes count by 1 to ensure that
// this value is correct in the next call of Decode.
dec.scan.bytes--
break Input
case scanEndObject, scanEndArray:
// scanEnd is delayed one byte.
Expand Down

0 comments on commit 0ac25d8

Please sign in to comment.