Skip to content

encoding/json: increment byte counter when using decoder.Token #43716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/encoding/json/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func (dec *Decoder) Token() (Token, error) {
return dec.tokenError(c)
}
dec.scanp++
dec.scan.bytes++
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
dec.tokenState = tokenArrayStart
return Delim('['), nil
Expand All @@ -388,6 +389,7 @@ func (dec *Decoder) Token() (Token, error) {
return dec.tokenError(c)
}
dec.scanp++
dec.scan.bytes++
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
dec.tokenValueEnd()
Expand All @@ -398,6 +400,7 @@ func (dec *Decoder) Token() (Token, error) {
return dec.tokenError(c)
}
dec.scanp++
dec.scan.bytes++
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
dec.tokenState = tokenObjectStart
return Delim('{'), nil
Expand All @@ -407,6 +410,7 @@ func (dec *Decoder) Token() (Token, error) {
return dec.tokenError(c)
}
dec.scanp++
dec.scan.bytes++
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
dec.tokenValueEnd()
Expand All @@ -417,17 +421,20 @@ func (dec *Decoder) Token() (Token, error) {
return dec.tokenError(c)
}
dec.scanp++
dec.scan.bytes++
dec.tokenState = tokenObjectValue
continue

case ',':
if dec.tokenState == tokenArrayComma {
dec.scanp++
dec.scan.bytes++
dec.tokenState = tokenArrayValue
continue
}
if dec.tokenState == tokenObjectComma {
dec.scanp++
dec.scan.bytes++
dec.tokenState = tokenObjectKey
continue
}
Expand Down Expand Up @@ -493,6 +500,7 @@ func (dec *Decoder) peek() (byte, error) {
for i := dec.scanp; i < len(dec.buf); i++ {
c := dec.buf[i]
if isSpace(c) {
dec.scan.bytes++
continue
}
dec.scanp = i
Expand Down
8 changes: 6 additions & 2 deletions src/encoding/json/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,14 @@ var tokenStreamCases = []tokenStreamCase{
}},
{json: `{ "\a" }`, expTokens: []interface{}{
Delim('{'),
&SyntaxError{"invalid character 'a' in string escape code", 3},
&SyntaxError{"invalid character 'a' in string escape code", 5},
}},
{json: ` \a`, expTokens: []interface{}{
&SyntaxError{"invalid character '\\\\' looking for beginning of value", 1},
&SyntaxError{"invalid character '\\\\' looking for beginning of value", 2},
}},
{json: `[$t]`, expTokens: []interface{}{
Delim('['),
&SyntaxError{"invalid character '$' looking for beginning of value", 2},
}},
}

Expand Down