diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-12-25 18:55:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-25 18:55:08 (GMT) |
commit | fc7e67f51a264c081a83ece59eed08d08cd250de (patch) | |
tree | 6327aabf2f9afda19742a5a80f2ba95c372c24ba /Modules | |
parent | bdad5c367f60d4939d6945f78d94608e4521009f (diff) | |
download | cpython-fc7e67f51a264c081a83ece59eed08d08cd250de.zip cpython-fc7e67f51a264c081a83ece59eed08d08cd250de.tar.gz cpython-fc7e67f51a264c081a83ece59eed08d08cd250de.tar.bz2 |
[3.12] bpo-37013: Fix the error handling in socket.if_indextoname() (GH-13503) (GH-112597)
* Fix a crash when pass UINT_MAX.
* Fix an integer overflow on 64-bit non-Windows platforms.
(cherry picked from commit 0daf555c6fb3feba77989382135a58215e1d70a5)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/socketmodule.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index de7229d..9724879 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -7085,17 +7085,23 @@ Returns the interface index corresponding to the interface name if_name."); 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; |