mirror of
https://github.com/golang/go
synced 2024-11-17 05:14:56 -07:00
cmd/go/internal/modfetch: avoid leaking a lockedfile.File in case of write errors
The go modules download command has a method called hashZip which checks the
hash of a zipped directory versus an expected value, and then writes it out
to a file. In the event that the write operation is not successful, we do
not close the file, leading to it being leaked. This could happen if the
user runs out of disk space, causing the underlying OS write command to
return an error. Ultimately, this led to a panic in lockfile.OpenFile which
was invoked from a finalizer garbage collecting the leaked file. The result
was a stack trace that didn't show the call stack from where the write
operation actually failed.
Fixes #50858
Change-Id: I4a24d2ab13dc903d623bbf8252b37bb9d724b8de
GitHub-Last-Rev: 354ef1d29e
GitHub-Pull-Request: golang/go#51058
Reviewed-on: https://go-review.googlesource.com/c/go/+/383915
Trust: Cherry Mui <cherryyz@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
b5af5c0834
commit
08ed4882aa
@ -319,7 +319,7 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
|
|||||||
//
|
//
|
||||||
// If the hash does not match go.sum (or the sumdb if enabled), hashZip returns
|
// If the hash does not match go.sum (or the sumdb if enabled), hashZip returns
|
||||||
// an error and does not write ziphashfile.
|
// an error and does not write ziphashfile.
|
||||||
func hashZip(mod module.Version, zipfile, ziphashfile string) error {
|
func hashZip(mod module.Version, zipfile, ziphashfile string) (err error) {
|
||||||
hash, err := dirhash.HashZip(zipfile, dirhash.DefaultHash)
|
hash, err := dirhash.HashZip(zipfile, dirhash.DefaultHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -331,16 +331,17 @@ func hashZip(mod module.Version, zipfile, ziphashfile string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if closeErr := hf.Close(); err == nil && closeErr != nil {
|
||||||
|
err = closeErr
|
||||||
|
}
|
||||||
|
}()
|
||||||
if err := hf.Truncate(int64(len(hash))); err != nil {
|
if err := hf.Truncate(int64(len(hash))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := hf.WriteAt([]byte(hash), 0); err != nil {
|
if _, err := hf.WriteAt([]byte(hash), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := hf.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user