fix: nil pointer panic in commonLock defer#4819
Open
fedebram wants to merge 1 commit intocontainerd:mainfrom
Open
fix: nil pointer panic in commonLock defer#4819fedebram wants to merge 1 commit intocontainerd:mainfrom
fedebram wants to merge 1 commit intocontainerd:mainfrom
Conversation
AkihiroSuda
reviewed
Apr 1, 2026
pkg/internal/filesystem/lock.go
Outdated
| defer func() { | ||
| if err != nil { | ||
| err = errors.Join(ErrLockFail, err, file.Close()) | ||
| err = errors.Join(ErrLockFail, err) |
Member
There was a problem hiding this comment.
The file still has to be closed unless it is nil
Author
There was a problem hiding this comment.
You're right, I'll add a nil check on the file.
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>
95d7ac8 to
5cc6265
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As discussed in #4802 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. Remove file.Close() from the defer function.