Skip to content

Commit 2aa3800

Browse files
committed
gh-156680: Raise the documented error from IPv6Network.next_network()
next_network() guarded address-space exhaustion with except OverflowError, which only int.to_bytes() on the IPv4 path raises. _BaseV6._string_from_ip_int() raises ValueError instead, so the handler never ran for IPv6 and the internal 'IPv6 address is too large' message escaped. Range-check next_ip against _ALL_ONES before formatting it, which decides the outcome for both address families before either path runs.
1 parent 852381e commit 2aa3800

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

Lib/ipaddress.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,15 +1150,15 @@ def next_network(self, next_prefix=None):
11501150
((new_netmask._ip & self.network_address._ip) >> bit_shift) + 1
11511151
) << bit_shift
11521152

1153-
try:
1154-
return self.__class__(
1155-
f"{self._string_from_ip_int(next_ip)}/{next_prefix}"
1156-
)
1157-
except OverflowError:
1153+
if next_ip > self._ALL_ONES:
11581154
raise ValueError(
11591155
f"out of address space, cannot make another /{next_prefix} "
11601156
"network"
1161-
) from None
1157+
)
1158+
1159+
return self.__class__(
1160+
f"{self._string_from_ip_int(next_ip)}/{next_prefix}"
1161+
)
11621162

11631163

11641164
class _BaseConstants:

Lib/test/test_ipaddress.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,9 +1596,15 @@ def testNextNetworkWithBadPrefix(self):
15961596

15971597
def testNextNetworkOutOfAddressSpace(self):
15981598
ipv4 = ipaddress.IPv4Network('255.255.255.0/24')
1599-
self.assertRaises(ValueError, ipv4.next_network)
1599+
self.assertRaisesRegex(
1600+
ValueError,
1601+
'out of address space, cannot make another /24 network',
1602+
ipv4.next_network)
16001603
ipv6 = ipaddress.IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112')
1601-
self.assertRaises(ValueError, ipv6.next_network)
1604+
self.assertRaisesRegex(
1605+
ValueError,
1606+
'out of address space, cannot make another /112 network',
1607+
ipv6.next_network)
16021608

16031609
def testFancySubnetting(self):
16041610
self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)),

0 commit comments

Comments
 (0)