Skip to content

Conversation

@Nepomuk5665
Copy link

Summary

  • Fix incorrect operator precedence in nil check that could cause panic

Description

The condition for checking container restart state in monitor.go had incorrect operator precedence.

The expression:

inspect.State != nil && inspect.State.Restarting || inspect.State.Running

is evaluated as:

(inspect.State != nil && inspect.State.Restarting) || inspect.State.Running

This means if inspect.State is nil, the inspect.State.Running check would be evaluated (because the left side of || could be false), causing a nil pointer dereference panic.

The fix adds explicit parentheses to ensure the nil check guards both state fields:

inspect.State != nil && (inspect.State.Restarting || inspect.State.Running)

Reproduction Scenario

This bug could be triggered when:

  1. A container dies (triggers the events.ActionDie case)
  2. ContainerInspect returns successfully but with State: nil (rare edge case)
  3. The container is not restarting (inspect.State.Restarting would panic before even checking)

While Docker typically always populates the State field, defensive programming dictates that after checking for nil, both conditions should be guarded.

Testing

  • All unit tests pass
  • Build completes successfully

@Nepomuk5665 Nepomuk5665 requested a review from a team as a code owner January 23, 2026 19:01
@Nepomuk5665 Nepomuk5665 requested review from glours and ndeloof January 23, 2026 19:01
@Nepomuk5665 Nepomuk5665 force-pushed the fix-nil-pointer-dereference-in-monitor branch from 2faa4e7 to cba3376 Compare January 23, 2026 19:01
The condition for checking container restart state had incorrect operator
precedence. The expression:

  inspect.State != nil && inspect.State.Restarting || inspect.State.Running

is evaluated as:

  (inspect.State != nil && inspect.State.Restarting) || inspect.State.Running

This means if inspect.State is nil and inspect.State.Restarting is false
(which would trigger a panic), the code would attempt to access
inspect.State.Running, causing a nil pointer dereference.

This fix adds parentheses to ensure the nil check applies to both
state checks:

  inspect.State != nil && (inspect.State.Restarting || inspect.State.Running)

Signed-off-by: Nepomuk Crhonek <105591323+Nepomuk5665@users.noreply.github.com>
@Nepomuk5665 Nepomuk5665 force-pushed the fix-nil-pointer-dereference-in-monitor branch from cba3376 to 635ab36 Compare January 23, 2026 19:28
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.

1 participant