Skip to content

Commit

Permalink
jar: close the zip.File reader before recursing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aktau committed Feb 28, 2022
1 parent 48d70bf commit bf524fa
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion jar/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ func (c *checker) checkFile(zf *zip.File, depth int, size int64, jar string) err
if err != nil {
return fmt.Errorf("open file %s: %v", p, err)
}
defer f.Close()
data, err := io.ReadAll(f)
f.Close() // Recycle the flate buffer earlier, we're going to recurse.
if err != nil {
return fmt.Errorf("read file %s: %v", p, err)
}
Expand Down

0 comments on commit bf524fa

Please sign in to comment.