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 @@ -979,7 +979,20 @@ def testInterfaceNameIndex(self):
979
979
'socket.if_indextoname() not available.' )
980
980
def testInvalidInterfaceIndexToName (self ):
981
981
self .assertRaises (OSError , socket .if_indextoname , 0 )
982
+ self .assertRaises (OverflowError , socket .if_indextoname , - 1 )
983
+ self .assertRaises (OverflowError , socket .if_indextoname , 2 ** 1000 )
982
984
self .assertRaises (TypeError , socket .if_indextoname , '_DEADBEEF' )
985
+ if hasattr (socket , 'if_nameindex' ):
986
+ indices = dict (socket .if_nameindex ())
987
+ for index in indices :
988
+ index2 = index + 2 ** 32
989
+ if index2 not in indices :
990
+ with self .assertRaises ((OverflowError , OSError )):
991
+ socket .if_indextoname (index2 )
992
+ for index in 2 ** 32 - 1 , 2 ** 64 - 1 :
993
+ if index not in indices :
994
+ with self .assertRaises ((OverflowError , OSError )):
995
+ socket .if_indextoname (index )
983
996
984
997
@unittest .skipUnless (hasattr (socket , 'if_nametoindex' ),
985
998
'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 @@ -6803,17 +6803,23 @@ Returns the interface index corresponding to the interface name if_name.");
6803
6803
static PyObject *
6804
6804
socket_if_indextoname (PyObject * self , PyObject * arg )
6805
6805
{
6806
+ unsigned long index_long = PyLong_AsUnsignedLong (arg );
6807
+ if (index_long == (unsigned long ) -1 && PyErr_Occurred ()) {
6808
+ return NULL ;
6809
+ }
6810
+
6806
6811
#ifdef MS_WINDOWS
6807
- NET_IFINDEX index ;
6812
+ NET_IFINDEX index = ( NET_IFINDEX ) index_long ;
6808
6813
#else
6809
- unsigned long index ;
6814
+ unsigned int index = ( unsigned int ) index_long ;
6810
6815
#endif
6811
- char name [IF_NAMESIZE + 1 ];
6812
6816
6813
- index = PyLong_AsUnsignedLong ( arg );
6814
- if ( index == ( unsigned long ) -1 )
6817
+ if (( unsigned long ) index != index_long ) {
6818
+ PyErr_SetString ( PyExc_OverflowError , " index is too large" );
6815
6819
return NULL ;
6820
+ }
6816
6821
6822
+ char name [IF_NAMESIZE + 1 ];
6817
6823
if (if_indextoname (index , name ) == NULL ) {
6818
6824
PyErr_SetFromErrno (PyExc_OSError );
6819
6825
return NULL ;
You can’t perform that action at this time.
0 commit comments