Skip to content

Commit 2975b27

Browse files
committed
scan: for style, adjust code for bad scan read counts
Make the code more consistent with the rest of the file. Should have caught this in review of CL 225357. Change-Id: I12824cb436539c31604684e043ebb7587cc92471 Reviewed-on: https://go-review.googlesource.com/c/go/+/225557 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
1 parent 93bcf91 commit 2975b27

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/bufio/scan.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
ErrTooLong = errors.New("bufio.Scanner: token too long")
7070
ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
7171
ErrAdvanceTooFar = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
72+
ErrBadReadCount = errors.New("bufio.Scanner: Read returned impossible count")
7273
)
7374

7475
const (
@@ -211,9 +212,9 @@ func (s *Scanner) Scan() bool {
211212
// be extra careful: Scanner is for safe, simple jobs.
212213
for loop := 0; ; {
213214
n, err := s.r.Read(s.buf[s.end:len(s.buf)])
214-
if n < 0 || n > len(s.buf)-s.end {
215-
n = 0
216-
err = errors.New("bufio.Scanner: Read returned impossible count")
215+
if n < 0 || len(s.buf)-s.end < n {
216+
s.setErr(ErrBadReadCount)
217+
break
217218
}
218219
s.end += n
219220
if err != nil {

0 commit comments

Comments
 (0)