Commit fbcaf99ac0 for golang
commit fbcaf99ac0b02fd6bfcaa7552cfb7c2ae767a227
Author: Guido Iaquinti <guido.iaquinti@gmail.com>
Date: Tue Dec 1 19:17:36 2026 +0000
net/netip: preserve the zero Addr in Next
Next is documented to return the zero Addr when there is no following
address, and the zero Addr has none. Prev already returns the zero Addr
for it, because its underflow check runs before subOne. Next calls
addOne first, so Addr{}.Next() returns an invalid Addr whose address
bits are 1.
That value breaks the package's assumption that the zero Addr is the
only invalid Addr: it is not equal to Addr{}, compares as greater than
Addr{}, is a separate map key, and turns into Addr{} after a
MarshalText/UnmarshalText round trip. A caller that checks the result
of Next against the zero Addr, as the documentation suggests, treats
it as a real address.
Return the zero Addr from Next for the zero Addr, as Prev does, and
test both methods on it.
Fixes #83551.
Change-Id: I938da56f1edf11d7859074ebe8fb0bdd8c7a10c4
GitHub-Last-Rev: f84d4d37acabd324744aca837a0ff2ee985f382c
GitHub-Pull-Request: golang/go#83440
Reviewed-on: https://go-review.googlesource.com/c/go/+/867803
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: David Chase <drchase@google.com>
diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go
index e2f7edf6f0..8d8648c219 100644
--- a/src/net/netip/netip.go
+++ b/src/net/netip/netip.go
@@ -743,6 +743,9 @@ func (ip Addr) AsSlice() []byte {
// Next returns the address following ip.
// If there is none, it returns the zero [Addr].
func (ip Addr) Next() Addr {
+ if !ip.IsValid() {
+ return Addr{}
+ }
ip.addr = ip.addr.addOne()
if ip.Is4() {
if uint32(ip.addr.lo) == 0 {
diff --git a/src/net/netip/netip_pkg_test.go b/src/net/netip/netip_pkg_test.go
index 2c9a2e6a8c..6bcea2d680 100644
--- a/src/net/netip/netip_pkg_test.go
+++ b/src/net/netip/netip_pkg_test.go
@@ -72,6 +72,7 @@ var nextPrevTests = []struct {
{mustIP("::%x"), mustIP("::1%x"), Addr{}},
{mustIP("::1"), mustIP("::2"), mustIP("::")},
{mustIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), Addr{}, mustIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")},
+ {Addr{}, Addr{}, Addr{}},
}
func TestIPNextPrev(t *testing.T) {