mirror of
https://github.com/python/cpython.git
synced 2025-07-15 23:35:23 +00:00
[3.10] bpo-37013: Fix the error handling in socket.if_indextoname() (GH-13503) (GH-112599)
* Fix a crash when pass UINT_MAX.
* Fix an integer overflow on 64-bit non-Windows platforms.
(cherry picked from commit 0daf555c6f
)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
parent
b6535ea7ec
commit
32e7acdc05
3 changed files with 31 additions and 9 deletions
|
@ -6827,17 +6827,23 @@ Returns the interface index corresponding to the interface name if_name.");
|
|||
static PyObject *
|
||||
socket_if_indextoname(PyObject *self, PyObject *arg)
|
||||
{
|
||||
#ifdef MS_WINDOWS
|
||||
NET_IFINDEX index;
|
||||
#else
|
||||
unsigned long index;
|
||||
#endif
|
||||
char name[IF_NAMESIZE + 1];
|
||||
|
||||
index = PyLong_AsUnsignedLong(arg);
|
||||
if (index == (unsigned long) -1)
|
||||
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_long;
|
||||
#else
|
||||
unsigned int index = (unsigned int)index_long;
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue