Proposal Details
The EOF
error is a common error that is reported in IO operations and is often exactly the right error at a finer granularity. However, when composed as part of a larger grammar or file format, many EOF errors should actually be converted into ErrUnexpectedEOF
. However, it is often forgotten, leading to bugs where parsing a truncated file or input silently fails. In fact, there have been several bugs of this nature even within the Go standard library:
* image/jpeg: return io.ErrUnexpectedEOF on truncated data
* compress/gzip: return unexpected EOF for certain truncated streams
* archive/tar: fix and cleanup readOldGNUSparseMap
* image/gif: check handling of truncated GIF files
...and certainly many bugs outside of the standard library (hence the motivation for this helper).
I propose the addition of the following helper function:
func NoEOF(err error) error {
if err == EOF {
return ErrUnexpectedEOF
}
return err
}
This won't inherently prevent bugs of this nature, but at least provides a helper making it easier to provide the appropriate fix. In our own code, we have the equivalent function declared in multiple places.
This proposal does not use errors.Is
based on the result of #39155.
Comment From: gabyhelp
Related Issues
- proposal: change standard library to check for io.EOF using errors.Is #39155 (closed)
- io: ReadFull should not forward io.ErrUnexpectedEOF from underlying reader #47677
- proposal: errors: add Ignore #74264
- proposal: strconv: Teach UnquoteChar to distinguish unexpected EOF from syntax errors #19107 (closed)
- encoding/binary: Read(U)varint should return ErrUnexpectedEOF #33575 (closed)
- io: use errors.Is in reader and writer helpers #53086 (closed)
Related Code Changes
- image/gif: check handling of truncated GIF files
- mime/multipart: change %v to %w for EOF error
- io: use errors.Is in reader and writer helpers
- all: Add errors handlings via errors package
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)