Skip to content

io.Copy(pgzip.Reader) returns io.EOF if stream already complete due to WriteTo implementation #38

@cyphar

Description

@cyphar

It turns out that if you have a pgzip.Reader which has read to the end of the
stream, if you call io.Copy on that stream you get io.EOF -- which should
never happen and causes spurious errors on callers that check error values from
io.Copy. I hit this when working on opencontainers/umoci#360.

This happens because (as an optimisation) io.Copy will use the WriteTo
method of the reader (or ReadFrom method of the writer) if they support that
method. And in this mode, io.Copy simply returns whatever error the reader or
writer give it -- meaning it doesn't hide io.EOFs returned from those
methods. In the normal Read+Write mode, io.Copy does hide the error.

It seems as though this is at some level a stdlib bug, because this requirement
of io.WriterTo and io.ReaderTo implementations (don't return io.EOF
because io.Copy can't handle it) is not spelled out anywhere in the
documentation. So either io.Copy should handle this, or this requirment
should be documented. So I will open a parallel issue on the Go tracker for
this problem.

But for now, it seems that the WriteTo implementation should avoid returning
io.EOF. If the reader reaches an io.EOF before it is expected, the error
should instead be io.ErrUnexpectedEOF.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions