cmd/go/internal/lockedfile: fix filelock.Unlock() called twice

filelock.Unlock() was called twice for fcntl implementation if an error
occurs during file.{,R}Lock(): once in the error handler, once in
filelock.lock().

Change-Id: I5ad84e8ef6b5e51d79e0a7a0c51f465276cd0c57
Reviewed-on: https://go-review.googlesource.com/c/152717
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Clément Chigot 2018-12-05 14:15:37 +01:00 committed by Bryan C. Mills
parent 5e1727892b
commit aac285c2df
1 changed files with 11 additions and 10 deletions

View File

@ -29,24 +29,25 @@ func openFile(name string, flag int, perm os.FileMode) (*os.File, error) {
default:
err = filelock.RLock(f)
}
if err == nil && flag&os.O_TRUNC == os.O_TRUNC {
if err = f.Truncate(0); err != nil {
if err != nil {
f.Close()
return nil, err
}
if flag&os.O_TRUNC == os.O_TRUNC {
if err := f.Truncate(0); err != nil {
// The documentation for os.O_TRUNC says “if possible, truncate file when
// opened”, but doesn't define “possible” (golang.org/issue/28699).
// We'll treat regular files (and symlinks to regular files) as “possible”
// and ignore errors for the rest.
if fi, statErr := f.Stat(); statErr == nil && !fi.Mode().IsRegular() {
err = nil
if fi, statErr := f.Stat(); statErr != nil || fi.Mode().IsRegular() {
filelock.Unlock(f)
f.Close()
return nil, err
}
}
}
if err != nil {
filelock.Unlock(f)
f.Close()
return nil, err
}
return f, nil
}