Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive/zip: File's reader returns io.EOF prematurely #32858

Open
dgrr opened this issue Jun 29, 2019 · 8 comments

Comments

@dgrr
Copy link

commented Jun 29, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.5 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"

What did you do?

The code: https://play.golang.org/p/JvKpGINHHBY

What did you expect to see?

Not a panic.
The panic doesn't happen if you read from an *os.File, *bufio.Reader or *bytes.Reader, it only happens with the Zip files because it prematurely returns the io.EOF error.

What did you see instead?

A panic.

Edit

https://go-review.googlesource.com/c/go/+/184122

@av86743

This comment has been minimized.

Copy link

commented Jun 29, 2019

Usually Gerrit changesets also contain test(s) for the fix.

@dgrr

This comment has been minimized.

Copy link
Author

commented Jun 29, 2019

@av86743 ok. I will add it tomorrow morning. Thanks

@odeke-em odeke-em changed the title zip.File's reader returns io.EOF prematurely archive/zip: File's reader returns io.EOF prematurely Jun 30, 2019

@odeke-em

This comment has been minimized.

Copy link
Member

commented Jun 30, 2019

Thank you for filing this bug @dgrr, and @av86743 for encouraging the test in the patchset!

@dgrr could you please perhaps explain the underlying problem and perhaps describe why the current behavior is wrong?

@av86743

This comment has been minimized.

Copy link

commented Jun 30, 2019

@odeke-em

archive/zip: zip.OpenReader panics when opened .zip file does not exist

as shown by provided example https://play.golang.org/p/JvKpGINHHBY

Panics similarly on /dev/null and /dev/zero as well.

@dgrr

This comment has been minimized.

Copy link
Author

commented Jun 30, 2019

Hello @odeke-em

The underlying problem is that the Read function in a zip.File returns the io.EOF error in the last read call, when readed bytes > 0.

I think that the current behavior is wrong based on how other Readers works. Also, the io.Reader doc says:

When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call.

@av86743

This comment has been minimized.

Copy link

commented Jun 30, 2019

Perhaps @odeke-em was going to suggest to add verbal description of a problem and/or solution to Gerrit issue, to facilitate the process of review?

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jun 30, 2019

@dgrr I'm sorry, I don't understand. The docs for io.Reader that you quote permit a Reader to return a non-zero number of bytes with io.EOF. Code that calls Read must always handle the bytes first, then consider the error.

The docs for io.Reader say

Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

It's not clear to me that there is any bug here.

@dgrr

This comment has been minimized.

Copy link
Author

commented Jul 1, 2019

Yes, you are right. But it's kind of weird to see how this Reader works as other readers works. From my point of view, I see that maybe the best option should be to return the io.EOF error in the subsequent call. But it's just a suggestion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.