Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,15 @@ def testGetaddrinfo(self):
except socket.gaierror:
pass

@support.cpython_only
def test_getaddrinfo_overflow(self):
# Issue #30710: test that getaddrinfo does not raise OverflowError
import _testcapi
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, _testcapi.LONG_MAX + 1)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, _testcapi.LONG_MIN - 1)

def test_getnameinfo(self):
# only IP addresses are allowed
self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ Kaartic Sivaraam
Ville Skyttä
Michael Sloan
Nick Sloan
Radek Smejkal
Václav Šmilauer
Christopher Smith
Eric V. Smith
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,9 @@ Library
- bpo-30177: path.resolve(strict=False) no longer cuts the path after the first
element not present in the filesystem. Patch by Antoine Pietri.

- bpo-30710: socket.getaddrinfo() now does not raise OverflowError if the
numeric port number is out of the C long range.

IDLE
----

Expand Down
14 changes: 9 additions & 5 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5947,7 +5947,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
struct addrinfo *res0 = NULL;
PyObject *hobj = NULL;
PyObject *pobj = (PyObject *)NULL;
char pbuf[30];
PyObject *pstr = NULL;
const char *hptr, *pptr;
int family, socktype, protocol, flags;
int error;
Expand Down Expand Up @@ -5977,11 +5977,13 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
return NULL;
}
if (PyLong_CheckExact(pobj)) {
long value = PyLong_AsLong(pobj);
if (value == -1 && PyErr_Occurred())
pstr = PyObject_Str(pobj);
if (pstr == NULL)
goto err;
assert(PyUnicode_Check(pstr));
pptr = PyUnicode_AsUTF8(pstr);
if (pptr == NULL)
goto err;
PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
pptr = pbuf;
} else if (PyUnicode_Check(pobj)) {
pptr = PyUnicode_AsUTF8(pobj);
if (pptr == NULL)
Expand Down Expand Up @@ -6040,12 +6042,14 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Py_XDECREF(single);
}
Py_XDECREF(idna);
Py_XDECREF(pstr);
if (res0)
freeaddrinfo(res0);
return all;
err:
Py_XDECREF(all);
Py_XDECREF(idna);
Py_XDECREF(pstr);
if (res0)
freeaddrinfo(res0);
return (PyObject *)NULL;
Expand Down