File tree 3 files changed +27
-5
lines changed
3 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -1066,7 +1066,20 @@ def testInterfaceNameIndex(self):
1066
1066
'socket.if_indextoname() not available.' )
1067
1067
def testInvalidInterfaceIndexToName (self ):
1068
1068
self .assertRaises (OSError , socket .if_indextoname , 0 )
1069
+ self .assertRaises (OverflowError , socket .if_indextoname , - 1 )
1070
+ self .assertRaises (OverflowError , socket .if_indextoname , 2 ** 1000 )
1069
1071
self .assertRaises (TypeError , socket .if_indextoname , '_DEADBEEF' )
1072
+ if hasattr (socket , 'if_nameindex' ):
1073
+ indices = dict (socket .if_nameindex ())
1074
+ for index in indices :
1075
+ index2 = index + 2 ** 32
1076
+ if index2 not in indices :
1077
+ with self .assertRaises ((OverflowError , OSError )):
1078
+ socket .if_indextoname (index2 )
1079
+ for index in 2 ** 32 - 1 , 2 ** 64 - 1 :
1080
+ if index not in indices :
1081
+ with self .assertRaises ((OverflowError , OSError )):
1082
+ socket .if_indextoname (index )
1070
1083
1071
1084
@unittest .skipUnless (hasattr (socket , 'if_nametoindex' ),
1072
1085
'socket.if_nametoindex() not available.' )
Original file line number Diff line number Diff line change
1
+ Fix a crash in :func: `socket.if_indextoname ` with specific value (UINT_MAX).
2
+ Fix an integer overflow in :func: `socket.if_indextoname ` on 64-bit
3
+ non-Windows platforms.
Original file line number Diff line number Diff line change @@ -6943,17 +6943,23 @@ Returns the interface index corresponding to the interface name if_name.");
6943
6943
static PyObject *
6944
6944
socket_if_indextoname (PyObject * self , PyObject * arg )
6945
6945
{
6946
+ unsigned long index_long = PyLong_AsUnsignedLong (arg );
6947
+ if (index_long == (unsigned long ) -1 && PyErr_Occurred ()) {
6948
+ return NULL ;
6949
+ }
6950
+
6946
6951
#ifdef MS_WINDOWS
6947
- NET_IFINDEX index ;
6952
+ NET_IFINDEX index = ( NET_IFINDEX ) index_long ;
6948
6953
#else
6949
- unsigned long index ;
6954
+ unsigned int index = ( unsigned int ) index_long ;
6950
6955
#endif
6951
- char name [IF_NAMESIZE + 1 ];
6952
6956
6953
- index = PyLong_AsUnsignedLong ( arg );
6954
- if ( index == ( unsigned long ) -1 )
6957
+ if (( unsigned long ) index != index_long ) {
6958
+ PyErr_SetString ( PyExc_OverflowError , " index is too large" );
6955
6959
return NULL ;
6960
+ }
6956
6961
6962
+ char name [IF_NAMESIZE + 1 ];
6957
6963
if (if_indextoname (index , name ) == NULL ) {
6958
6964
PyErr_SetFromErrno (PyExc_OSError );
6959
6965
return NULL ;
You can’t perform that action at this time.
0 commit comments