Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit bf524fa

Browse files
committed
jar: close the zip.File reader before recursing
Package archive/zip has a pool [1] for flate buffers. Calling `Close` on the result of `zip.(*File).Open` returns the buffer to the pool. We should do this as soon as we are able (when we no longer need the file), especially before recursing. I did not modify the `defer f.Close()` in the code paths above because: 1. The `return` follows closely after (no recursion) in their case. And: 2. They actually use the file for longer. [1]: https://cs.opensource.google/go/go/+/master:src/archive/zip/register.go;l=69;drc=1b09d430678d4a6f73b2443463d11f75851aba8a.
1 parent 48d70bf commit bf524fa

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

jar/jar.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ func (c *checker) checkFile(zf *zip.File, depth int, size int64, jar string) err
385385
if err != nil {
386386
return fmt.Errorf("open file %s: %v", p, err)
387387
}
388-
defer f.Close()
389388
data, err := io.ReadAll(f)
389+
f.Close() // Recycle the flate buffer earlier, we're going to recurse.
390390
if err != nil {
391391
return fmt.Errorf("read file %s: %v", p, err)
392392
}

0 commit comments

Comments
 (0)