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
bufio: Scanner.Scan panics on poorly behaved io.Reader during an error #38053
Comments
I don't think it's necessary for |
I made an edit to my original report that I do wan to explicitly call out. This is ostensibly a wrapper of |
Alternatively, I was thinking about some way for |
This is a tricky one. Normally I agree with @ianlancetaylor , and this case is no exception, but then there's this comment in the code:
That tells me that it's worth adding the check on the next line for a non-negative n. |
OK, sent CL 225357. |
Change https://golang.org/cl/225357 mentions this issue: |
Change https://golang.org/cl/237739 mentions this issue: |
Change https://golang.org/cl/238217 mentions this issue: |
…rgeReader CL 225357 added tests for Scanner not panicking on bad readers. CL 225557 created a named error value that is returned instead. CL 237739 documents that the bufio.ErrBadReadCount is returned when bufio.Scanner is used with an invalid io.Reader. This suggests we wouldn't want that behavior to be able to change without a test noticing it, so modify the tests to check for the exact error value instead of just any non-nil one. For #38053. Change-Id: I4b0b8eb6804ebfe2c768505ddb94f0b1017fcf8b Reviewed-on: https://go-review.googlesource.com/c/go/+/238217 Reviewed-by: Ian Lance Taylor <iant@golang.org>
For #37419 For #38053 Change-Id: I206f360ff4957bc7edc3c35dfc814b7bd5ec440c Reviewed-on: https://go-review.googlesource.com/c/go/+/237739 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
If an
io.Reader
incorrectly returns-1
for bytes read along with an error fromRead
,bufio.Scanner
will panic indexing the slice here.Here's a simple repro on the go playground
This was discovered with a real project that's exposing native serial port APIs as
io.Reader
s. There's PRs open fix the implemenation, however, it may be useful forbufio.Scanner
to be resilient to this failure mode. Especially sincegolang.org/x/sys/unix.Read
can return negative values under error, as is the case here.What did you expect to see?
The error from the
Read
call to bubble up and be returned fromScanner.Err()
.What did you see instead?
The scanner panics
The text was updated successfully, but these errors were encountered: