From abbc5f25a519ae32d9bde7117c491aadc42d59f6 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 1 Feb 2023 17:49:27 +0000 Subject: [PATCH] zip: fix File.Open not working if called after zip.Extract completes 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. --- zip.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zip.go b/zip.go index c10b93cf..30e7b5b1 100644 --- a/zip.go +++ b/zip.go @@ -202,6 +202,7 @@ func (z Zip) Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchiv skipDirs := skipList{} for i, f := range zr.File { + f := f // make a copy for the Open closure if err := ctx.Err(); err != nil { return err // honor context cancellation }