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

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

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

dgrr opened this issue Jun 29, 2019 · 8 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@dgrr
Copy link

dgrr 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
Copy link

av86743 commented Jun 29, 2019

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

@dgrr
Copy link
Author

dgrr 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
Copy link
Member

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?

@odeke-em odeke-em added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 30, 2019
@av86743
Copy link

av86743 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
Copy link
Author

dgrr 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
Copy link

av86743 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
Copy link
Contributor

@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
Copy link
Author

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

@golang golang locked and limited conversation to collaborators Oct 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

6 participants