FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-41004: Resolve hash collisions for IPv4Interface and IPv6Interface by rvteja92 · Pull Request #21033 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions Lib/ipaddress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ def __lt__(self, other):
return False

def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
return hash((self._ip, self._prefixlen, int(self.network.network_address)))

__reduce__ = _IPAddressBase.__reduce__

Expand Down Expand Up @@ -2120,7 +2120,7 @@ def __lt__(self, other):
return False

def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
return hash((self._ip, self._prefixlen, int(self.network.network_address)))

__reduce__ = _IPAddressBase.__reduce__

Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_ipaddress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,24 @@ def testsixtofour(self):
sixtofouraddr.sixtofour)
self.assertFalse(bad_addr.sixtofour)

# issue41004 Hash collisions in IPv4Interface and IPv6Interface

Comment thread
rvteja92 marked this conversation as resolved.
Outdated
def testV4HashIsNotConstant(self):
ipv4_address1 = ipaddress.IPv4Interface("1.2.3.4")
ipv4_address2 = ipaddress.IPv4Interface("2.3.4.5")
self.assertNotEqual(32, ipv4_address1.__hash__())
Comment thread
rvteja92 marked this conversation as resolved.
Outdated
self.assertNotEqual(32, ipv4_address2.__hash__())
self.assertNotEqual(ipv4_address1.__hash__(), ipv4_address2.__hash__())

# issue41004 Hash collisions in IPv4Interface and IPv6Interface

Comment thread
rvteja92 marked this conversation as resolved.
Outdated
def testV6HashIsNotConstant(self):
ipv6_address1 = ipaddress.IPv6Interface("2001:658:22a:cafe:200:0:0:1")
ipv6_address2 = ipaddress.IPv6Interface("2001:658:22a:cafe:200:0:0:2")
self.assertNotEqual(128, ipv6_address1.__hash__())
self.assertNotEqual(128, ipv6_address2.__hash__())
Comment thread
rvteja92 marked this conversation as resolved.
Outdated
self.assertNotEqual(ipv6_address1.__hash__(), ipv6_address2.__hash__())


if __name__ == '__main__':
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The hash() methods of classes IPv4Interface and IPv6Interface (of the ipaddress library) had issue of generating constant hash values of 32 and 128 respectively causing hash collisions.
Comment thread
rvteja92 marked this conversation as resolved.
Outdated
The fix uses the hash() function to generate hash values for the objects instead of XOR operation
Comment thread
rvteja92 marked this conversation as resolved.
Outdated

Back | FazBrowse Home | New Git URL