summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2023-12-01 15:16:49 (GMT)
committerGitHub <noreply@github.com>2023-12-01 15:16:49 (GMT)
commit0daf555c6fb3feba77989382135a58215e1d70a5 (patch)
tree0b7b09098b294c8406796e3851afe0ac00655c0b /Modules
parent70a38ffb3d712f973eb17bd1bda541f238ae70d2 (diff)
downloadcpython-0daf555c6fb3feba77989382135a58215e1d70a5.zip
cpython-0daf555c6fb3feba77989382135a58215e1d70a5.tar.gz
cpython-0daf555c6fb3feba77989382135a58215e1d70a5.tar.bz2
bpo-37013: Fix the error handling in socket.if_indextoname() (GH-13503)
* Fix a crash when pass UINT_MAX. * Fix an integer overflow on 64-bit non-Windows platforms.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/socketmodule.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 9ac2001..0a0e0e7 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -7071,17 +7071,23 @@ _socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname)
static PyObject *
socket_if_indextoname(PyObject *self, PyObject *arg)
{
+ unsigned long index_long = PyLong_AsUnsignedLong(arg);
+ if (index_long == (unsigned long) -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+
#ifdef MS_WINDOWS
- NET_IFINDEX index;
+ NET_IFINDEX index = (NET_IFINDEX)index_long;
#else
- unsigned long index;
+ unsigned int index = (unsigned int)index_long;
#endif
- char name[IF_NAMESIZE + 1];
- index = PyLong_AsUnsignedLong(arg);
- if (index == (unsigned long) -1)
+ if ((unsigned long)index != index_long) {
+ PyErr_SetString(PyExc_OverflowError, "index is too large");
return NULL;
+ }
+ char name[IF_NAMESIZE + 1];
if (if_indextoname(index, name) == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;