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 @@ -1070,7 +1070,20 @@ def testInterfaceNameIndex(self):
1070
1070
'socket.if_indextoname() not available.' )
1071
1071
def testInvalidInterfaceIndexToName (self ):
1072
1072
self .assertRaises (OSError , socket .if_indextoname , 0 )
1073
+ self .assertRaises (OverflowError , socket .if_indextoname , - 1 )
1074
+ self .assertRaises (OverflowError , socket .if_indextoname , 2 ** 1000 )
1073
1075
self .assertRaises (TypeError , socket .if_indextoname , '_DEADBEEF' )
1076
+ if hasattr (socket , 'if_nameindex' ):
1077
+ indices = dict (socket .if_nameindex ())
1078
+ for index in indices :
1079
+ index2 = index + 2 ** 32
1080
+ if index2 not in indices :
1081
+ with self .assertRaises ((OverflowError , OSError )):
1082
+ socket .if_indextoname (index2 )
1083
+ for index in 2 ** 32 - 1 , 2 ** 64 - 1 :
1084
+ if index not in indices :
1085
+ with self .assertRaises ((OverflowError , OSError )):
1086
+ socket .if_indextoname (index )
1074
1087
1075
1088
@unittest .skipUnless (hasattr (socket , 'if_nametoindex' ),
1076
1089
'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 @@ -6890,17 +6890,23 @@ Returns the interface index corresponding to the interface name if_name.");
6890
6890
static PyObject *
6891
6891
socket_if_indextoname (PyObject * self , PyObject * arg )
6892
6892
{
6893
+ unsigned long index_long = PyLong_AsUnsignedLong (arg );
6894
+ if (index_long == (unsigned long ) -1 && PyErr_Occurred ()) {
6895
+ return NULL ;
6896
+ }
6897
+
6893
6898
#ifdef MS_WINDOWS
6894
- NET_IFINDEX index ;
6899
+ NET_IFINDEX index = ( NET_IFINDEX ) index_long ;
6895
6900
#else
6896
- unsigned long index ;
6901
+ unsigned int index = ( unsigned int ) index_long ;
6897
6902
#endif
6898
- char name [IF_NAMESIZE + 1 ];
6899
6903
6900
- index = PyLong_AsUnsignedLong ( arg );
6901
- if ( index == ( unsigned long ) -1 )
6904
+ if (( unsigned long ) index != index_long ) {
6905
+ PyErr_SetString ( PyExc_OverflowError , " index is too large" );
6902
6906
return NULL ;
6907
+ }
6903
6908
6909
+ char name [IF_NAMESIZE + 1 ];
6904
6910
if (if_indextoname (index , name ) == NULL ) {
6905
6911
PyErr_SetFromErrno (PyExc_OSError );
6906
6912
return NULL ;
You can’t perform that action at this time.
0 commit comments