Skip to content

fix: nil pointer panic in commonLock defer#4819

Open
fedebram wants to merge 1 commit intocontainerd:mainfrom
fedebram:fix-commonlock-nil-pointer
Open

fix: nil pointer panic in commonLock defer#4819
fedebram wants to merge 1 commit intocontainerd:mainfrom
fedebram:fix-commonlock-nil-pointer

Conversation

@fedebram
Copy link
Copy Markdown

@fedebram fedebram commented Apr 1, 2026

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.

defer func() {
if err != nil {
err = errors.Join(ErrLockFail, err, file.Close())
err = errors.Join(ErrLockFail, err)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file still has to be closed unless it is nil

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll add a nil check on the file.

Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@fedebram fedebram closed this Apr 2, 2026
@fedebram fedebram force-pushed the fix-commonlock-nil-pointer branch from 95d7ac8 to 5cc6265 Compare April 2, 2026 14:13
@fedebram fedebram reopened this Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants