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 @@ -1082,7 +1082,20 @@ def testInterfaceNameIndex(self):
1082
1082
'socket.if_indextoname() not available.' )
1083
1083
def testInvalidInterfaceIndexToName (self ):
1084
1084
self .assertRaises (OSError , socket .if_indextoname , 0 )
1085
+ self .assertRaises (OverflowError , socket .if_indextoname , - 1 )
1086
+ self .assertRaises (OverflowError , socket .if_indextoname , 2 ** 1000 )
1085
1087
self .assertRaises (TypeError , socket .if_indextoname , '_DEADBEEF' )
1088
+ if hasattr (socket , 'if_nameindex' ):
1089
+ indices = dict (socket .if_nameindex ())
1090
+ for index in indices :
1091
+ index2 = index + 2 ** 32
1092
+ if index2 not in indices :
1093
+ with self .assertRaises ((OverflowError , OSError )):
1094
+ socket .if_indextoname (index2 )
1095
+ for index in 2 ** 32 - 1 , 2 ** 64 - 1 :
1096
+ if index not in indices :
1097
+ with self .assertRaises ((OverflowError , OSError )):
1098
+ socket .if_indextoname (index )
1086
1099
1087
1100
@unittest .skipUnless (hasattr (socket , 'if_nametoindex' ),
1088
1101
'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 @@ -7085,17 +7085,23 @@ Returns the interface index corresponding to the interface name if_name.");
7085
7085
static PyObject *
7086
7086
socket_if_indextoname (PyObject * self , PyObject * arg )
7087
7087
{
7088
+ unsigned long index_long = PyLong_AsUnsignedLong (arg );
7089
+ if (index_long == (unsigned long ) -1 && PyErr_Occurred ()) {
7090
+ return NULL ;
7091
+ }
7092
+
7088
7093
#ifdef MS_WINDOWS
7089
- NET_IFINDEX index ;
7094
+ NET_IFINDEX index = ( NET_IFINDEX ) index_long ;
7090
7095
#else
7091
- unsigned long index ;
7096
+ unsigned int index = ( unsigned int ) index_long ;
7092
7097
#endif
7093
- char name [IF_NAMESIZE + 1 ];
7094
7098
7095
- index = PyLong_AsUnsignedLong ( arg );
7096
- if ( index == ( unsigned long ) -1 )
7099
+ if (( unsigned long ) index != index_long ) {
7100
+ PyErr_SetString ( PyExc_OverflowError , " index is too large" );
7097
7101
return NULL ;
7102
+ }
7098
7103
7104
+ char name [IF_NAMESIZE + 1 ];
7099
7105
if (if_indextoname (index , name ) == NULL ) {
7100
7106
PyErr_SetFromErrno (PyExc_OSError );
7101
7107
return NULL ;
You can’t perform that action at this time.
0 commit comments