Skip to content
This repository was 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
mholt merged 1 commit into
mholt:masterfrom
ncw:fix-zip-open
Feb 1, 2023
Merged

mholt merged 1 commit into
mholt:masterfrom
ncw:fix-zip-open

Conversation

@ncw

@ncw ncw commented Feb 1, 2023

Copy link
Copy Markdown
Contributor

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

ncw commented Feb 1, 2023

Copy link
Copy Markdown
Contributor Author

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!

@mholt mholt left a comment

Copy link
Copy Markdown
Owner

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

mholt commented Jun 11, 2024

Copy link
Copy Markdown
Owner

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

⌂ 换网址