From 5cc6265e84c9819d4651962c4e69c1713aedc0bc Mon Sep 17 00:00:00 2001 From: Federico Bramucci <163430291+fedebram@users.noreply.github.com> Date: Wed, 1 Apr 2026 18:16:13 +0200 Subject: [PATCH] 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> --- pkg/internal/filesystem/lock.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/internal/filesystem/lock.go b/pkg/internal/filesystem/lock.go index d993e380b41..c03d711cec1 100644 --- a/pkg/internal/filesystem/lock.go +++ b/pkg/internal/filesystem/lock.go @@ -51,7 +51,11 @@ func ReadOnlyLock(path string) (file *os.File, err error) { func commonlock(path string, mode lockType) (file *os.File, err error) { defer func() { if err != nil { - err = errors.Join(ErrLockFail, err, file.Close()) + if file != nil { + err = errors.Join(ErrLockFail, err, file.Close()) + } else { + err = errors.Join(ErrLockFail, err) + } } }()