Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb30ee26

Browse files
authored
bpo-41004: Resolve hash collisions for IPv4Interface and IPv6Interface (GH-21033)
The __hash__() methods of classes IPv4Interface and IPv6Interface had issueof generating constant hash values of 32 and 128 respectively causing hash collisions.The fix uses the hash() function to generate hash values for the objectsinstead of XOR operation
1 parenta3ad95d commitb30ee26

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

‎Lib/ipaddress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ def __lt__(self, other):
14201420
returnFalse
14211421

14221422
def__hash__(self):
1423-
returnself._ip^self._prefixlen^int(self.network.network_address)
1423+
returnhash((self._ip,self._prefixlen,int(self.network.network_address)))
14241424

14251425
__reduce__=_IPAddressBase.__reduce__
14261426

@@ -2120,7 +2120,7 @@ def __lt__(self, other):
21202120
returnFalse
21212121

21222122
def__hash__(self):
2123-
returnself._ip^self._prefixlen^int(self.network.network_address)
2123+
returnhash((self._ip,self._prefixlen,int(self.network.network_address)))
21242124

21252125
__reduce__=_IPAddressBase.__reduce__
21262126

‎Lib/test/test_ipaddress.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,18 @@ def testsixtofour(self):
25482548
sixtofouraddr.sixtofour)
25492549
self.assertFalse(bad_addr.sixtofour)
25502550

2551+
# issue41004 Hash collisions in IPv4Interface and IPv6Interface
2552+
deftestV4HashIsNotConstant(self):
2553+
ipv4_address1=ipaddress.IPv4Interface("1.2.3.4")
2554+
ipv4_address2=ipaddress.IPv4Interface("2.3.4.5")
2555+
self.assertNotEqual(ipv4_address1.__hash__(),ipv4_address2.__hash__())
2556+
2557+
# issue41004 Hash collisions in IPv4Interface and IPv6Interface
2558+
deftestV6HashIsNotConstant(self):
2559+
ipv6_address1=ipaddress.IPv6Interface("2001:658:22a:cafe:200:0:0:1")
2560+
ipv6_address2=ipaddress.IPv6Interface("2001:658:22a:cafe:200:0:0:2")
2561+
self.assertNotEqual(ipv6_address1.__hash__(),ipv6_address2.__hash__())
2562+
25512563

25522564
if__name__=='__main__':
25532565
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The __hash__() methods of ipaddress.IPv4Interface and ipaddress.IPv6Interface incorrectly generated constant hash values of 32 and 128 respectively. This resulted in always causing hash collisions. The fix uses hash() to generate hash values for the tuple of (address, mask length, network address).

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp