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

compress/gzip: Header problems when .Close() is deferred #20075

Closed
niaow opened this issue Apr 21, 2017 · 3 comments
Closed

compress/gzip: Header problems when .Close() is deferred #20075

niaow opened this issue Apr 21, 2017 · 3 comments

Comments

@niaow
Copy link

niaow commented Apr 21, 2017

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

go 1.8.1

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

linux amd64

What did you do?

I put .Close() for a gzip writer in a defer statement (writing to a file).

The simplest example I found that replicates this behavior is here: https://play.golang.org/p/WmZtqtvnyN
And if I swap the defer for a non-deferred close: https://play.golang.org/p/KAdLfmUlHQ
NOTE: I originally did this with a file. This is not just for bytes.Buffer

What did you expect to see?

The data is successfully saved.

What did you see instead?

Gzip output is corrupted.

@bradfitz
Copy link
Contributor

Your program is buggy. You're capturing the buffer before you call Writer.Close, but the Close does the final flush and writes the final bit of the gzip. Also, don't ignore the return value of Close whenever writing is involved.

@bradfitz
Copy link
Contributor

For questions about Go, see https://golang.org/wiki/Questions.

@niaow
Copy link
Author

niaow commented Apr 22, 2017

Ok nvm removing my check of the return value of close fixed it

@golang golang locked and limited conversation to collaborators Apr 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants