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

zip: fix File.Open not working if called after zip.Extract completes #367

Merged
merged 1 commit into from
Feb 1, 2023

Conversation

ncw
Copy link
Contributor

@ncw ncw commented Feb 1, 2023

The zip.Extract method creates a closure for the File.Open function
pointer sent to the handler method.

Unfortunately it uses the same File for each call to the handler
function which means that if the caller stores the File objects and
uses them after Extract has returned then each File.Open function
pointer will open the same object.

This is easily fixed by making sure that each iteration of the Extract
loop uses a new File variable.

The zip.Extract method creates a closure for the File.Open function
pointer sent to the handler method.

Unfortunately it uses the same File for each call to the handler
function which means that if the caller stores the File objects and
uses them after Extract has returned then each File.Open function
pointer will open the same object.

This is easily fixed by making sure that each iteration of the Extract
loop uses a new File variable.
@ncw
Copy link
Contributor Author

ncw commented Feb 1, 2023

I found this bug when working on the rclone backend for the archiver: See rclone/rclone#2815

Yes this is a lovely example of a classic Go gotcha!

Copy link
Owner

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah wow! I have started seeing static check / linter warnings for stuff like this in my editor recently. Thanks for the patch Nick!

@mholt mholt merged commit 10a6421 into mholt:master Feb 1, 2023
@ncw ncw deleted the fix-zip-open branch February 1, 2023 19:41
@mholt
Copy link
Owner

mholt commented Jun 11, 2024

Memo to self: I don't think this is needed after Go 1.22 anymore: https://tip.golang.org/doc/go1.22#language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants