Skip to content

Commit

Permalink
fix regression where Net6.Count() returns 0 for ::/0
Browse files Browse the repository at this point in the history
  • Loading branch information
c-robinson committed Jan 2, 2024
1 parent 182edbf commit 5947051
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 4 additions & 0 deletions net6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
11 changes: 4 additions & 7 deletions net6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"net"
"sort"
"testing"

"lukechampine.com/uint128"
)

var NewNet6Tests = []struct {
Expand Down Expand Up @@ -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",
},
}

Expand All @@ -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" {
Expand All @@ -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())
}
}
}
Expand Down

0 comments on commit 5947051

Please sign in to comment.