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

Related Code Changes

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)