Avoid panic on nil container Config/NetworkSettings/HostConfig#748
Open
JamBalaya56562 wants to merge 1 commit into
Open
Avoid panic on nil container Config/NetworkSettings/HostConfig#748JamBalaya56562 wants to merge 1 commit into
JamBalaya56562 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens container inspection and address rendering against nil docker.Container sub-struct pointers returned by some Docker daemons/remote APIs, preventing panics that would otherwise abort template generation for all containers.
Changes:
- In
getContainers, copyConfig,NetworkSettings, andHostConfiginto zero-value locals when their pointers arenil, and use the locals for field access. - Make
context.GetContainerAddresses/renderAddresstolerant ofnilNetworkSettings(andnilConfigfor the exposed-ports fallback). - Add tests covering
nilstruct pointers for bothgetContainersandGetContainerAddresses.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/generator/generator.go | Avoid nil-pointer dereferences during container inspect processing by using zero-value copies. |
| internal/generator/generator_test.go | Add regression test ensuring nil Config/NetworkSettings/HostConfig doesn’t panic during getContainers. |
| internal/context/address.go | Make address rendering and port enumeration nil-safe for missing NetworkSettings/Config. |
| internal/context/address_test.go | Add tests ensuring GetContainerAddresses returns empty (and doesn’t panic) when struct pointers are nil. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
955f216 to
6417e1b
Compare
Contributor
Author
|
Addressed Copilot's review feedback — renamed the shadowing |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
6417e1b to
5d61367
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.
getContainersdereferencedcontainer.Config,container.NetworkSettingsandcontainer.HostConfig(andGetContainerAddresses/renderAddressdereferencedNetworkSettings/Config) without nil checks. Inspect can return these as nil for some containers/daemons, causing a panic that aborts generation for every container.This copies each pointer into a zero value when nil, so the container is still emitted with its ID/Name and empty fields instead of crashing, and makes
GetContainerAddressesnil-safe (it's exported and used elsewhere).Stateis a value (not a pointer) and ranging nil maps/slices is safe, so those are left unchanged.Adds nil-struct tests for
GetContainerAddressesandgetContainers.Fixes #227
🤖 Generated with Claude Code