-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
net/textproto: swallows errors returned by underlying reader #53858
Comments
Your code never returns an error. There is nothing for bufio to report.
Closing as there is nothing to do. For questions please refer to https://github.com/golang/go/wiki/Questions |
@seankhliao it does, see line 29 |
Your |
@seankhliao I updated the example code: https://go.dev/play/p/XD0e4msIGqt |
Thanks, I do think there is a bug here. |
@ianlancetaylor One quick fix I can think of is to add one more field in In the case of using a non-autorewind reader, after reading the last line of message with In the case of using autorewind reader, the Please point out if there anything unclear or unthoughtful above, thanks |
The bufio package is pretty low level and it would be much better if we didn't have to change it. As far as I can see the bug here is in net/textproto, and that is where the fix should occur. |
Could I make an attempt at fixing this issue? |
The existing implementation of ReadMIMEHeader() relies on the assumption that bufio will keep the EOF error. The for loop will not terminates until a EOF occurs or a len 0 buf is read. In the case where a rewind reader is use, bufio will always return a string and ignore the EOF error. Hence an variable is ued to indicate that EOF is reached. A corresponding test cases is provided. Fix golang#53858
Change https://go.dev/cl/418694 mentions this issue: |
Change https://go.dev/cl/421554 mentions this issue: |
Change https://go.dev/cl/544935 mentions this issue: |
Looking again at the original example, the func (r *autoRewind) Read(p []byte) (int, error) {
if r.r == nil {
r.r = strings.NewReader(r.buf)
}
n, err := r.r.Read(p)
fmt.Printf("%d: %v\n", n, err)
if err == io.EOF {
// rewind
r.r = strings.NewReader(r.buf)
}
return n, err
} This is going to return
That is, after returning So now I'm not sure whether there is a bug here at all. |
Thanks for looking this one more time @ianlancetaylor. Re-reading the thread again as well as your last comment, I see what @seankhliao was saying. I agree, the example implementation doesn't do that. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
See example: https://go.dev/play/p/Bn8H6p4QUf6
What did you expect to see?
I expect that bufio package react on underlying reader errors and stop the reading.
What did you see instead?
I see that bufio skips an error multiple times, and the example code causes OOM error becuase reader is rewinded every time:
here are cases when an error is not tracked:
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/net/textproto/reader.go;l=148;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/net/textproto/reader.go;l=496;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/net/textproto/reader.go;l=566;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/net/textproto/reader.go;l=571;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
the error, returned by autoRewind is just ignored and set to nil:
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/bufio/bufio.go;l=156;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/bufio/bufio.go;l=124;drc=27794c4d4a18c61d8c158d253421d72b5a6a8673
I know that it's something that I have to expect with autoRewind, but this behavior caused an unpleasant surprise.
see also:
The text was updated successfully, but these errors were encountered: