From 5947051ad020281279c590f4b50f30c071253408 Mon Sep 17 00:00:00 2001 From: Chad Robinson Date: Tue, 2 Jan 2024 01:16:16 -0800 Subject: [PATCH] fix regression where Net6.Count() returns 0 for ::/0 --- net6.go | 4 ++++ net6_test.go | 11 ++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/net6.go b/net6.go index 1a5aef1..42be02b 100644 --- a/net6.go +++ b/net6.go @@ -111,6 +111,10 @@ func (n Net6) Count() uint128.Uint128 { moreOnes, _ := n.Hostmask.Size() exp -= moreOnes + if exp == 128 { + return uint128.Max + } + z := uint128.New(2, 0) return z.Lsh(uint(exp - 1)) } diff --git a/net6_test.go b/net6_test.go index 6124c10..c17ee76 100644 --- a/net6_test.go +++ b/net6_test.go @@ -4,8 +4,6 @@ import ( "net" "sort" "testing" - - "lukechampine.com/uint128" ) var NewNet6Tests = []struct { @@ -220,13 +218,13 @@ var Net6Tests = []struct { "2001:0db8::", "::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - 0, -1, 0, "340282366920938463463374607431768211456", + 0, -1, 0, "340282366920938463463374607431768211455", }, { "::", "::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - 0, -1, 0, "340282366920938463463374607431768211456", + 0, -1, 0, "340282366920938463463374607431768211455", }, } @@ -242,7 +240,6 @@ func TestNet6_Version(t *testing.T) { func TestNet6_Count(t *testing.T) { for i, tt := range Net6Tests { ipn := NewNet6(net.ParseIP(tt.ip), tt.netmasklen, tt.hostmask) - ttcount, _ := uint128.FromString(tt.count) if ipn.IPNet.IP == nil { if tt.count != "0" { @@ -251,8 +248,8 @@ func TestNet6_Count(t *testing.T) { continue } - if v := ttcount.Cmp(ipn.Count()); v != 0 { - t.Errorf("[%d] count: want %s got %d", i, tt.count, ipn.Count()) + if tt.count != ipn.Count().String() { + t.Errorf("[%d] count: want %s got %s", i, tt.count, ipn.Count().String()) } } }