api/types/network: fix handling of unmapped ports (ephemeral ports)

commit 4c24542e95 changed `PortRange.All()`
to omit zero values for ports, but this caused a regression; the zero-value
is used in some places to assign an ephemeral port, e.g.: `--port 80` is an
implicit `--port 0:80`, or `--port <ephemeral port>:80`, where the daemon
picks a random port number from the ephemeral port range as host-port.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-04-02 11:49:01 +02:00
parent 74b72d82aa
commit b8057a3a58
3 changed files with 12 additions and 7 deletions

View File

@@ -328,9 +328,11 @@ func (pr PortRange) Range() PortRange {
// }
func (pr PortRange) All() iter.Seq[Port] {
return func(yield func(Port) bool) {
if pr.proto == protoZero {
return
}
// Do not skip zero values here, because a zero-value means
// "map the port to an ephemeral host port".
//
// For example, "--port 80" is shorthand for "--port 0:80"
// ("--port <ephemeral port>:80").
for i := uint32(pr.Start()); i <= uint32(pr.End()); i++ {
if !yield(Port{num: uint16(i), proto: pr.proto}) {
return

View File

@@ -278,7 +278,8 @@ func TestPortRange(t *testing.T) {
assert.Equal(t, pr.End(), uint16(0))
assert.Equal(t, pr.Proto(), network.IPProtocol(""))
assert.Equal(t, pr.Range(), pr)
assert.Check(t, slices.Equal(slices.Collect(pr.All()), []network.Port{}))
var ephemeralPort network.Port
assert.Check(t, slices.Equal(slices.Collect(pr.All()), []network.Port{ephemeralPort}))
t.Run("Marshal Unmarshal", func(t *testing.T) {
var pr network.PortRange

View File

@@ -328,9 +328,11 @@ func (pr PortRange) Range() PortRange {
// }
func (pr PortRange) All() iter.Seq[Port] {
return func(yield func(Port) bool) {
if pr.proto == protoZero {
return
}
// Do not skip zero values here, because a zero-value means
// "map the port to an ephemeral host port".
//
// For example, "--port 80" is shorthand for "--port 0:80"
// ("--port <ephemeral port>:80").
for i := uint32(pr.Start()); i <= uint32(pr.End()); i++ {
if !yield(Port{num: uint16(i), proto: pr.proto}) {
return