Skip to content

proposal: io: add NoEOF to convert EOF to ErrUnexpectedEOF #74265

Open
@dsnet

Description

@dsnet

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:

...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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions