-
Notifications
You must be signed in to change notification settings - Fork 18.4k
io: add Err field to LimitedReader #76156
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
base: master
Are you sure you want to change the base?
Conversation
Add an Err field to LimitedReader that allows callers to return a custom error when the read limit is exceeded, instead of always returning EOF. When Err is set to a non-nil, non-EOF value, and the limit is reached, LimitedReader.Read probes the underlying reader with a 1-byte read to distinguish two cases: stream had exactly N bytes (returns EOF), or stream has more data (returns the custom Err). The probe result is cached using negative N values to avoid repeated reads. When Err is nil or EOF, Read returns EOF, maintaining backward compatibility. Zero-length reads return (0, nil) without side effects. Fixes golang#51115
|
This PR (HEAD: 9b6b14c) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/717340. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Sergei G: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Ian Lance Taylor: Patch Set 2: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
This PR (HEAD: 0796ec8) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/717340. Important tips:
|
|
This PR (HEAD: ebf18d7) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/717340. Important tips:
|
|
This PR (HEAD: 50cc465) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/717340. Important tips:
|
|
Message from Sergei G: Patch Set 3: (7 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Ian Lance Taylor: Patch Set 5: Code-Review+1 Commit-Queue+1 (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 5: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2025-11-05T01:49:44Z","revision":"8f4a8f03537b552c336b14542e34df5ba7bd4eb0"} Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Ian Lance Taylor: Patch Set 5: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 5: This CL has failed the run. Reason: Tryjob golang/try/x_tools-gotip-linux-amd64 has failed with summary (view all results):
To reproduce, try Additional links for debugging: Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 5: LUCI-TryBot-Result-1 Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
This PR (HEAD: bd71dde) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/717340. Important tips:
|
|
Message from Sergei G: Patch Set 5: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Ian Lance Taylor: Patch Set 7: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 7: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2025-11-05T05:14:20Z","revision":"11cda9e1f2980d03180b65889f79fd43725c27d5"} Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Ian Lance Taylor: Patch Set 7: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 7: This CL has failed the run. Reason: Tryjob golang/try/gotip-linux-amd64_avx512 has failed with summary (view all results):
To reproduce, try Additional links for debugging: Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
|
Message from Go LUCI: Patch Set 7: LUCI-TryBot-Result-1 Please don’t reply on this GitHub thread. Visit golang.org/cl/717340. |
Add an Err field to LimitedReader that allows callers to return a
custom error when the read limit is exceeded, instead of always
returning EOF.
When Err is set to a non-nil, and the limit is reached,
LimitedReader.Read probes the underlying reader with a 1-byte
read to distinguish two cases: stream had exactly N bytes
(returns EOF), or stream has more data. The probe result is cached
using negative N values to avoid repeated reads.
When Err is nil, Read returns EOF, maintaining backward compatibility.
Zero-length reads return (0, nil) without side effects when we
reached end of Reader.
Fixes #51115