Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants