diff --git a/src/encoding/json/stream.go b/src/encoding/json/stream.go index 7d5137fbc7..e29127499b 100644 --- a/src/encoding/json/stream.go +++ b/src/encoding/json/stream.go @@ -92,20 +92,23 @@ func (dec *Decoder) readValue() (int, error) { scanp := dec.scanp var err error Input: - for { + // help the compiler see that scanp is never negative, so it can remove + // some bounds checks below. + for scanp >= 0 { + // Look in the buffer for a new value. - for i, c := range dec.buf[scanp:] { + for ; scanp < len(dec.buf); scanp++ { + c := dec.buf[scanp] dec.scan.bytes++ switch dec.scan.step(&dec.scan, c) { case scanEnd: - scanp += i break Input case scanEndObject, scanEndArray: // scanEnd is delayed one byte. // We might block trying to get that byte from src, // so instead invent a space byte. if stateEndValue(&dec.scan, ' ') == scanEnd { - scanp += i + 1 + scanp++ break Input } case scanError: @@ -113,7 +116,6 @@ Input: return 0, dec.scan.err } } - scanp = len(dec.buf) // Did the last read have an error? // Delayed until now to allow buffer scan.