Skip to content

Commit

Permalink
fix: IP Add/Sub overflow or underflow
Browse files Browse the repository at this point in the history
Signed-off-by: zcq98 <[email protected]>
  • Loading branch information
zcq98 committed May 31, 2024
1 parent 45c1e47 commit f940fd2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/ipam/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ func bytes2IP(buff []byte, length int) IP {
}

func (a IP) Add(n int64) IP {
return bytes2IP(big.NewInt(0).Add(big.NewInt(0).SetBytes([]byte(a)), big.NewInt(n)).Bytes(), len(a))
if a.To4() != nil {
return bytes2IP(big.NewInt(0).Add(big.NewInt(0).SetBytes([]byte(a.To4())), big.NewInt(n)).Bytes(), len(a.To4()))
}
return bytes2IP(big.NewInt(0).Add(big.NewInt(0).SetBytes([]byte(a.To16())), big.NewInt(n)).Bytes(), len(a.To16()))
}

func (a IP) Sub(n int64) IP {
return bytes2IP(big.NewInt(0).Sub(big.NewInt(0).SetBytes([]byte(a)), big.NewInt(n)).Bytes(), len(a))
ipInt := big.NewInt(0).SetBytes(a.To16())
ipInt.Sub(ipInt, big.NewInt(n))
if ipInt.Sign() < 0 {
ipInt.Add(ipInt, big.NewInt(0).Exp(big.NewInt(2), big.NewInt(128), nil))
}

if a.To4() != nil {
return bytes2IP(ipInt.Bytes(), len(a.To4()))
}
return bytes2IP(ipInt.Bytes(), len(a.To16()))
}

func (a IP) String() string {
Expand Down
36 changes: 36 additions & 0 deletions pkg/ipam/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,24 @@ func TestIPAdd(t *testing.T) {
n: 1,
want: IP(net.ParseIP("192.168.1.2")),
},
{
name: "IPv4 add 2",
a: IP(net.ParseIP("192.168.1.1")),
n: 10,
want: IP(net.ParseIP("192.168.1.11")),
},
{
name: "IPv6 add",
a: IP(net.ParseIP("2001:db8::1")),
n: 1,
want: IP(net.ParseIP("2001:db8::2")),
},
{
name: "IPv6 add 2",
a: IP(net.ParseIP("1:db8::1")),
n: 1,
want: IP(net.ParseIP("1:db8::2")),
},
{
name: "IPv4 add overflow",
a: IP(net.ParseIP("255.255.255.255")),
Expand All @@ -163,6 +175,12 @@ func TestIPAdd(t *testing.T) {
n: 2,
want: IP(net.ParseIP("0.0.0.0")),
},
{
name: "IPv4 add overflow 3",
a: IP(net.ParseIP("255.255.255.254")),
n: 3,
want: IP(net.ParseIP("0.0.0.1")),
},
{
name: "IPv6 add overflow",
a: IP(net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
Expand All @@ -175,6 +193,12 @@ func TestIPAdd(t *testing.T) {
n: 2,
want: IP(net.ParseIP("::")),
},
{
name: "IPv6 add overflow 3",
a: IP(net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")),
n: 3,
want: IP(net.ParseIP("::1")),
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -217,6 +241,12 @@ func TestIPSub(t *testing.T) {
n: 2,
want: IP(net.ParseIP("255.255.255.255")),
},
{
name: "IPv4 sub underflow 3",
a: IP(net.ParseIP("0.0.0.1")),
n: 3,
want: IP(net.ParseIP("255.255.255.254")),
},
{
name: "IPv6 sub underflow",
a: IP(net.ParseIP("::")),
Expand All @@ -229,6 +259,12 @@ func TestIPSub(t *testing.T) {
n: 2,
want: IP(net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
},
{
name: "IPv6 sub underflow 3",
a: IP(net.ParseIP("::1")),
n: 3,
want: IP(net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")),
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f940fd2

Please sign in to comment.