@@ -1600,6 +1600,54 @@ def testGetaddrinfo(self):
1600
1600
except socket .gaierror :
1601
1601
pass
1602
1602
1603
+ def test_getaddrinfo_int_port_overflow (self ):
1604
+ # gh-74895: Test that getaddrinfo does not raise OverflowError on port.
1605
+ #
1606
+ # POSIX getaddrinfo() never specify the valid range for "service"
1607
+ # decimal port number values. For IPv4 and IPv6 they are technically
1608
+ # unsigned 16-bit values, but the API is protocol agnostic. Which values
1609
+ # trigger an error from the C library function varies by platform as
1610
+ # they do not all perform validation.
1611
+
1612
+ # The key here is that we don't want to produce OverflowError as Python
1613
+ # prior to 3.12 did for ints outside of a [LONG_MIN, LONG_MAX] range.
1614
+ # Leave the error up to the underlying string based platform C API.
1615
+
1616
+ from _testcapi import ULONG_MAX , LONG_MAX , LONG_MIN
1617
+ try :
1618
+ socket .getaddrinfo (None , ULONG_MAX + 1 )
1619
+ except OverflowError :
1620
+ # Platforms differ as to what values consitute a getaddrinfo() error
1621
+ # return. Some fail for LONG_MAX+1, others ULONG_MAX+1, and Windows
1622
+ # silently accepts such huge "port" aka "service" numeric values.
1623
+ self .fail ("Either no error or socket.gaierror expected." )
1624
+ except socket .gaierror :
1625
+ pass
1626
+
1627
+ try :
1628
+ socket .getaddrinfo (None , LONG_MAX + 1 )
1629
+ except OverflowError :
1630
+ self .fail ("Either no error or socket.gaierror expected." )
1631
+ except socket .gaierror :
1632
+ pass
1633
+
1634
+ try :
1635
+ socket .getaddrinfo (None , LONG_MAX - 0xffff + 1 )
1636
+ except OverflowError :
1637
+ self .fail ("Either no error or socket.gaierror expected." )
1638
+ except socket .gaierror :
1639
+ pass
1640
+
1641
+ try :
1642
+ socket .getaddrinfo (None , LONG_MIN - 1 )
1643
+ except OverflowError :
1644
+ self .fail ("Either no error or socket.gaierror expected." )
1645
+ except socket .gaierror :
1646
+ pass
1647
+
1648
+ socket .getaddrinfo (None , 0 ) # No error expected.
1649
+ socket .getaddrinfo (None , 0xffff ) # No error expected.
1650
+
1603
1651
def test_getnameinfo (self ):
1604
1652
# only IP addresses are allowed
1605
1653
self .assertRaises (OSError , socket .getnameinfo , ('mail.python.org' ,0 ), 0 )
0 commit comments