You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since Go 1.16 in std no code that uses bytes.ErrTooLarge.
Panicking with sentinel error is a bad example given the standard library to Go's newcomers. It leads to logic around of panic-recover instead of using error flow and errors.Is.
I ask you to pay attention to this.
History of changes:
@robpike introduced bytes.ErrTooLarge as sentinel error with error flow (link).
@robpike supported ioutil.ReadAll and introduced panicking (link).
@rsc rewrote ioutil.ReadAll and moved it in io (link). Now in io no dependency to bytes.ErrTooLarge.
P.S.
This code
funcmakeSlice(nint) []byte {
// If the make fails, give a known error.deferfunc() {
ifrecover() !=nil {
panic(ErrTooLarge)
}
}()
returnmake([]byte, n)
}
In general a very large (*bytes.Buffer).Grow can fail in one of two ways: it can run the process out of memory (which causes an unrecoverable runtime throw), or it can fail to even compute a sensible size (which is what results in the aforementioned panic.
Both of those outcomes result from programmer errors, and should be avoidable through sensible bounds-checks on the caller side. So I don't see any benefit to removing the existing panic.
@bcmills I have nothing against panic in this case, but I'm confused by panic with public error.
What logic is expected for this error on the package client's side?
The proposed change is reasonable but it's not backward compatible. I think that making this change would violate the Go 1 compatibility guarantee.
I'm going to close this issue. Please comment if you disagree.
To answer your question, the client side is not expected to create a buffer so large that it can't be represented. For buffers that can grow without bounds, add a bounds check. Code is not expected to catch this panic, it's expected to avoid the panicking case.
Hello!
Since Go 1.16 in std no code that uses
bytes.ErrTooLarge
.Panicking with sentinel error is a bad example given the standard library to Go's newcomers. It leads to logic around of
panic
-recover
instead of using error flow anderrors.Is
.I ask you to pay attention to this.
History of changes:
bytes.ErrTooLarge
as sentinel error with error flow (link).ioutil.ReadAll
and introduced panicking (link).ioutil.ReadAll
and moved it inio
(link). Now inio
no dependency tobytes.ErrTooLarge
.P.S.
This code
can be rewrited as
But I have no idea about:
The text was updated successfully, but these errors were encountered: