Skip to content

Commit 4cf3ee9

Browse files
ggaaooppeennggmvdan
authored andcommitted
encoding/json: fix byte counter increments when using decoder.Token()
At every iteration of decoder.Token(), scaner.bytes does not increment when it meets spaces and other non value token. Signed-off-by: Peng Gao <[email protected]> Change-Id: Id41b7e22e2c45fffc7fafacd618724f0f214dd2f
1 parent 43afb1a commit 4cf3ee9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/encoding/json/stream.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ func (dec *Decoder) Token() (Token, error) {
379379
return dec.tokenError(c)
380380
}
381381
dec.scanp++
382+
dec.scan.bytes++
382383
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
383384
dec.tokenState = tokenArrayStart
384385
return Delim('['), nil
@@ -388,6 +389,7 @@ func (dec *Decoder) Token() (Token, error) {
388389
return dec.tokenError(c)
389390
}
390391
dec.scanp++
392+
dec.scan.bytes++
391393
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
392394
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
393395
dec.tokenValueEnd()
@@ -398,6 +400,7 @@ func (dec *Decoder) Token() (Token, error) {
398400
return dec.tokenError(c)
399401
}
400402
dec.scanp++
403+
dec.scan.bytes++
401404
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
402405
dec.tokenState = tokenObjectStart
403406
return Delim('{'), nil
@@ -407,6 +410,7 @@ func (dec *Decoder) Token() (Token, error) {
407410
return dec.tokenError(c)
408411
}
409412
dec.scanp++
413+
dec.scan.bytes++
410414
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
411415
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
412416
dec.tokenValueEnd()
@@ -417,17 +421,20 @@ func (dec *Decoder) Token() (Token, error) {
417421
return dec.tokenError(c)
418422
}
419423
dec.scanp++
424+
dec.scan.bytes++
420425
dec.tokenState = tokenObjectValue
421426
continue
422427

423428
case ',':
424429
if dec.tokenState == tokenArrayComma {
425430
dec.scanp++
431+
dec.scan.bytes++
426432
dec.tokenState = tokenArrayValue
427433
continue
428434
}
429435
if dec.tokenState == tokenObjectComma {
430436
dec.scanp++
437+
dec.scan.bytes++
431438
dec.tokenState = tokenObjectKey
432439
continue
433440
}
@@ -493,6 +500,7 @@ func (dec *Decoder) peek() (byte, error) {
493500
for i := dec.scanp; i < len(dec.buf); i++ {
494501
c := dec.buf[i]
495502
if isSpace(c) {
503+
dec.scan.bytes++
496504
continue
497505
}
498506
dec.scanp = i

src/encoding/json/stream_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,14 @@ var tokenStreamCases = []tokenStreamCase{
397397
}},
398398
{json: `{ "\a" }`, expTokens: []interface{}{
399399
Delim('{'),
400-
&SyntaxError{"invalid character 'a' in string escape code", 3},
400+
&SyntaxError{"invalid character 'a' in string escape code", 5},
401401
}},
402402
{json: ` \a`, expTokens: []interface{}{
403-
&SyntaxError{"invalid character '\\\\' looking for beginning of value", 1},
403+
&SyntaxError{"invalid character '\\\\' looking for beginning of value", 2},
404+
}},
405+
{json: `[$t]`, expTokens: []interface{}{
406+
Delim('['),
407+
&SyntaxError{"invalid character '$' looking for beginning of value", 2},
404408
}},
405409
}
406410

0 commit comments

Comments
 (0)