Skip to content

Commit 5cc6265

Browse files
committed
Fix nil pointer panic in commonLock defer
The defer function in commonLock calls file.Close() when an error occurs, but file is always nil at that point. If os.Open or os.OpenFile fails, file is nil. If platformSpecificLock fails, file is closed inline and then set to nil by the return statement. In both cases, calling file.Close() in the defer panics on a nil pointer. Add nil check before file.Close() in the defer. Signed-off-by: Federico Bramucci <163430291+fedebram@users.noreply.github.com>
1 parent 0951079 commit 5cc6265

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pkg/internal/filesystem/lock.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func ReadOnlyLock(path string) (file *os.File, err error) {
5151
func commonlock(path string, mode lockType) (file *os.File, err error) {
5252
defer func() {
5353
if err != nil {
54-
err = errors.Join(ErrLockFail, err, file.Close())
54+
if file != nil {
55+
err = errors.Join(ErrLockFail, err, file.Close())
56+
} else {
57+
err = errors.Join(ErrLockFail, err)
58+
}
5559
}
5660
}()
5761

0 commit comments

Comments
 (0)