summaryrefslogtreecommitdiffstats
path: root/Modules
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 /Modules
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 'Modules')
-rw-r--r--Modules/getaddrinfo.c6
-rw-r--r--Modules/socketmodule.c14
2 files changed, 14 insertions, 6 deletions
diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c
index 0b4620e..f1c28d7 100644
--- a/Modules/getaddrinfo.c
+++ b/Modules/getaddrinfo.c
@@ -342,7 +342,11 @@ getaddrinfo(const char*hostname, const char*servname,
pai->ai_socktype = SOCK_DGRAM;
pai->ai_protocol = IPPROTO_UDP;
}
- port = htons((u_short)atoi(servname));
+ long maybe_port = strtol(servname, NULL, 10);
+ if (maybe_port < 0 || maybe_port > 0xffff) {
+ ERR(EAI_SERVICE);
+ }
+ port = htons((u_short)maybe_port);
} else {
struct servent *sp;
const char *proto;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 0a9e465..2d300f1 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -6650,7 +6650,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;
@@ -6680,11 +6680,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)
@@ -6750,12 +6752,14 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Py_DECREF(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;