summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRadek Smejkal <radek.smejkal@atrak.cz>2023-02-14 01:37:34 (GMT)
committerGitHub <noreply@github.com>2023-02-14 01:37:34 (GMT)
commit928752ce4c23f47d3175dd47ecacf08d86a99c9d (patch)
tree65124d98543829099a38f1d8fc8b33644a352de2 /Lib/test
parent0c6fe81dce9d6bb1dce5e4503f1b42bc5355ba24 (diff)
downloadcpython-928752ce4c23f47d3175dd47ecacf08d86a99c9d.zip
cpython-928752ce4c23f47d3175dd47ecacf08d86a99c9d.tar.gz
cpython-928752ce4c23f47d3175dd47ecacf08d86a99c9d.tar.bz2
gh-74895: getaddrinfo no longer raises OverflowError (#2435)
`socket.getaddrinfo()` no longer raises `OverflowError` based on the **port** argument. Error reporting (or not) for its value is left up to the underlying C library `getaddrinfo()` implementation.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_socket.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index f1b4018..a313da2 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1600,6 +1600,54 @@ class GeneralModuleTests(unittest.TestCase):
except socket.gaierror:
pass
+ def test_getaddrinfo_int_port_overflow(self):
+ # gh-74895: Test that getaddrinfo does not raise OverflowError on port.
+ #
+ # POSIX getaddrinfo() never specify the valid range for "service"
+ # decimal port number values. For IPv4 and IPv6 they are technically
+ # unsigned 16-bit values, but the API is protocol agnostic. Which values
+ # trigger an error from the C library function varies by platform as
+ # they do not all perform validation.
+
+ # The key here is that we don't want to produce OverflowError as Python
+ # prior to 3.12 did for ints outside of a [LONG_MIN, LONG_MAX] range.
+ # Leave the error up to the underlying string based platform C API.
+
+ from _testcapi import ULONG_MAX, LONG_MAX, LONG_MIN
+ try:
+ socket.getaddrinfo(None, ULONG_MAX + 1)
+ except OverflowError:
+ # Platforms differ as to what values consitute a getaddrinfo() error
+ # return. Some fail for LONG_MAX+1, others ULONG_MAX+1, and Windows
+ # silently accepts such huge "port" aka "service" numeric values.
+ self.fail("Either no error or socket.gaierror expected.")
+ except socket.gaierror:
+ pass
+
+ try:
+ socket.getaddrinfo(None, LONG_MAX + 1)
+ except OverflowError:
+ self.fail("Either no error or socket.gaierror expected.")
+ except socket.gaierror:
+ pass
+
+ try:
+ socket.getaddrinfo(None, LONG_MAX - 0xffff + 1)
+ except OverflowError:
+ self.fail("Either no error or socket.gaierror expected.")
+ except socket.gaierror:
+ pass
+
+ try:
+ socket.getaddrinfo(None, LONG_MIN - 1)
+ except OverflowError:
+ self.fail("Either no error or socket.gaierror expected.")
+ except socket.gaierror:
+ pass
+
+ socket.getaddrinfo(None, 0) # No error expected.
+ socket.getaddrinfo(None, 0xffff) # No error expected.
+
def test_getnameinfo(self):
# only IP addresses are allowed
self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0)