Skip to content

Commit c5a6fb6

Browse files
[3.5] bpo-29931 fix __lt__ check in ipaddress.ip_interface for both v4 and v6. (GH-879) (#2218)
the original logic was just comparing the network address but this is wrong because if the network address is equal then we need to compare the ip address for breaking the tie add more ip_interface comparison tests. (cherry picked from commit 7bd8d3e)
1 parent b39c78a commit c5a6fb6

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Lib/ipaddress.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,8 @@ def __lt__(self, other):
14101410
if address_less is NotImplemented:
14111411
return NotImplemented
14121412
try:
1413-
return self.network < other.network
1413+
return (self.network < other.network or
1414+
self.network == other.network and address_less)
14141415
except AttributeError:
14151416
# We *do* allow addresses and interfaces to be sorted. The
14161417
# unassociated address is considered less than all interfaces.
@@ -2100,7 +2101,8 @@ def __lt__(self, other):
21002101
if address_less is NotImplemented:
21012102
return NotImplemented
21022103
try:
2103-
return self.network < other.network
2104+
return (self.network < other.network or
2105+
self.network == other.network and address_less)
21042106
except AttributeError:
21052107
# We *do* allow addresses and interfaces to be sorted. The
21062108
# unassociated address is considered less than all interfaces.

Lib/test/test_ipaddress.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,14 +1404,35 @@ def testAddressComparison(self):
14041404
ipaddress.ip_address('::2'))
14051405

14061406
def testInterfaceComparison(self):
1407-
self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1408-
ipaddress.ip_interface('1.1.1.1'))
1409-
self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1410-
ipaddress.ip_interface('1.1.1.2'))
1411-
self.assertTrue(ipaddress.ip_interface('::1') <=
1412-
ipaddress.ip_interface('::1'))
1413-
self.assertTrue(ipaddress.ip_interface('::1') <=
1414-
ipaddress.ip_interface('::2'))
1407+
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') ==
1408+
ipaddress.ip_interface('1.1.1.1/24'))
1409+
self.assertTrue(ipaddress.ip_interface('1.1.1.1/16') <
1410+
ipaddress.ip_interface('1.1.1.1/24'))
1411+
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') <
1412+
ipaddress.ip_interface('1.1.1.2/24'))
1413+
self.assertTrue(ipaddress.ip_interface('1.1.1.2/16') <
1414+
ipaddress.ip_interface('1.1.1.1/24'))
1415+
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') >
1416+
ipaddress.ip_interface('1.1.1.1/16'))
1417+
self.assertTrue(ipaddress.ip_interface('1.1.1.2/24') >
1418+
ipaddress.ip_interface('1.1.1.1/24'))
1419+
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') >
1420+
ipaddress.ip_interface('1.1.1.2/16'))
1421+
1422+
self.assertTrue(ipaddress.ip_interface('::1/64') ==
1423+
ipaddress.ip_interface('::1/64'))
1424+
self.assertTrue(ipaddress.ip_interface('::1/64') <
1425+
ipaddress.ip_interface('::1/80'))
1426+
self.assertTrue(ipaddress.ip_interface('::1/64') <
1427+
ipaddress.ip_interface('::2/64'))
1428+
self.assertTrue(ipaddress.ip_interface('::2/48') <
1429+
ipaddress.ip_interface('::1/64'))
1430+
self.assertTrue(ipaddress.ip_interface('::1/80') >
1431+
ipaddress.ip_interface('::1/64'))
1432+
self.assertTrue(ipaddress.ip_interface('::2/64') >
1433+
ipaddress.ip_interface('::1/64'))
1434+
self.assertTrue(ipaddress.ip_interface('::1/64') >
1435+
ipaddress.ip_interface('::2/48'))
14151436

14161437
def testNetworkComparison(self):
14171438
# ip1 and ip2 have the same network address

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Extension Modules
5656
Library
5757
-------
5858

59+
- bpo-29931: Fixed comparison check for ipaddress.ip_interface objects.
60+
Patch by Sanjay Sundaresan.
61+
5962
- [Security] bpo-29591: Update expat copy from 2.1.1 to 2.2.0 to get fixes
6063
of CVE-2016-0718 and CVE-2016-4472. See
6164
https://sourceforge.net/p/expat/bugs/537/ for more information.

0 commit comments

Comments
 (0)