daemon: fix linting S1016 (staticcheck) false positive

Staticcheck is suggesting to cast the type or to directly copy, but
doesn't account for nat.SortPortMap mutating the second argument, so
mutating the HostConfig.PortBindings. From the code, it looks like the
intent here was to prevent that (creating a deep copy), so let's keep
that.

    daemon/container_operations.go:109:39: S1016: should convert bb (type github.com/docker/docker/vendor/github.com/docker/go-connections/nat.PortBinding) to github.com/docker/docker/vendor/github.com/moby/moby/api/types/container.PortBinding instead of using struct literal (staticcheck)
                    bindings[p] = append(bindings[p], containertypes.PortBinding{
                                                      ^
    daemon/network.go:952:39: S1016: should convert bb (type github.com/docker/docker/vendor/github.com/docker/go-connections/nat.PortBinding) to github.com/docker/docker/vendor/github.com/moby/moby/api/types/container.PortBinding instead of using struct literal (staticcheck)
                    bindings[p] = append(bindings[p], containertypes.PortBinding{
                                                      ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-07-31 02:57:23 +02:00
parent d58dc493fe
commit 5bbf3af980
2 changed files with 14 additions and 20 deletions

View File

@@ -101,17 +101,14 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet
}
}
// Create a deep copy (as [nat.SortPortMap] mutates the map).
// Not using a maps.Clone here, as that won't dereference the
// slice (PortMap is a map[Port][]PortBinding).
bindings := make(containertypes.PortMap)
if ctr.HostConfig.PortBindings != nil {
for p, b := range ctr.HostConfig.PortBindings {
bindings[p] = []containertypes.PortBinding{}
for _, bb := range b {
bindings[p] = append(bindings[p], containertypes.PortBinding{
HostIP: bb.HostIP,
HostPort: bb.HostPort,
})
}
}
for p, b := range ctr.HostConfig.PortBindings {
copied := make([]containertypes.PortBinding, len(b))
copy(copied, b)
bindings[p] = copied
}
// TODO(thaJeztah): Move this code to a method on nat.PortSet.