]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/lockedfile: fix filelock.Unlock() called twice
authorClément Chigot <clement.chigot@atos.net>
Wed, 5 Dec 2018 13:15:37 +0000 (14:15 +0100)
committerBryan C. Mills <bcmills@google.com>
Wed, 5 Dec 2018 16:09:05 +0000 (16:09 +0000)
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>
src/cmd/go/internal/lockedfile/lockedfile_filelock.go

index 1c390f7425d0aea6eabbcc1faf8aee14a033ae2e..f63dd8664b0ab12e2038d746c9744f57187b4b24 100644 (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
 }