@@ -6650,14 +6650,17 @@ static PyObject *
66506650os_setgroups (PyObject * module , PyObject * groups )
66516651/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
66526652{
6653- int i , len ;
6653+ Py_ssize_t i , len ;
66546654 gid_t grouplist [MAX_GROUPS ];
66556655
66566656 if (!PySequence_Check (groups )) {
66576657 PyErr_SetString (PyExc_TypeError , "setgroups argument must be a sequence" );
66586658 return NULL ;
66596659 }
66606660 len = PySequence_Size (groups );
6661+ if (len < 0 ) {
6662+ return NULL ;
6663+ }
66616664 if (len > MAX_GROUPS ) {
66626665 PyErr_SetString (PyExc_ValueError , "too many groups" );
66636666 return NULL ;
@@ -7886,9 +7889,9 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length)
78867889#if (defined(HAVE_SENDFILE ) && (defined(__FreeBSD__ ) || defined(__DragonFly__ ) \
78877890 || defined(__APPLE__ ))) || defined(HAVE_READV ) || defined(HAVE_WRITEV )
78887891static Py_ssize_t
7889- iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , int cnt , int type )
7892+ iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , Py_ssize_t cnt , int type )
78907893{
7891- int i , j ;
7894+ Py_ssize_t i , j ;
78927895 Py_ssize_t blen , total = 0 ;
78937896
78947897 * iov = PyMem_New (struct iovec , cnt );
@@ -7965,8 +7968,7 @@ static Py_ssize_t
79657968os_readv_impl (PyObject * module , int fd , PyObject * buffers )
79667969/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
79677970{
7968- int cnt ;
7969- Py_ssize_t n ;
7971+ Py_ssize_t cnt , n ;
79707972 int async_err = 0 ;
79717973 struct iovec * iov ;
79727974 Py_buffer * buf ;
@@ -7978,6 +7980,8 @@ os_readv_impl(PyObject *module, int fd, PyObject *buffers)
79787980 }
79797981
79807982 cnt = PySequence_Size (buffers );
7983+ if (cnt < 0 )
7984+ return -1 ;
79817985
79827986 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_WRITABLE ) < 0 )
79837987 return -1 ;
@@ -8116,15 +8120,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
81168120 "sendfile() headers must be a sequence" );
81178121 return NULL ;
81188122 } else {
8119- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8120- sf .hdr_cnt = PySequence_Size (headers );
8121- if (sf .hdr_cnt > 0 &&
8122- (i = iov_setup (& (sf .headers ), & hbuf ,
8123- headers , sf .hdr_cnt , PyBUF_SIMPLE )) < 0 )
8123+ Py_ssize_t i = PySequence_Size (headers );
8124+ if (i < 0 )
8125+ return NULL ;
8126+ if (i > INT_MAX ) {
8127+ PyErr_SetString (PyExc_OverflowError ,
8128+ "sendfile() header is too large" );
81248129 return NULL ;
8130+ }
8131+ if (i > 0 ) {
8132+ sf .hdr_cnt = (int )i ;
8133+ i = iov_setup (& (sf .headers ), & hbuf ,
8134+ headers , sf .hdr_cnt , PyBUF_SIMPLE );
8135+ if (i < 0 )
8136+ return NULL ;
81258137#ifdef __APPLE__
8126- sbytes += i ;
8138+ sbytes += i ;
81278139#endif
8140+ }
81288141 }
81298142 }
81308143 if (trailers != NULL) {
@@ -8133,15 +8146,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
81338146 "sendfile() trailers must be a sequence" );
81348147 return NULL ;
81358148 } else {
8136- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8137- sf .trl_cnt = PySequence_Size (trailers );
8138- if (sf .trl_cnt > 0 &&
8139- (i = iov_setup (& (sf .trailers ), & tbuf ,
8140- trailers , sf .trl_cnt , PyBUF_SIMPLE )) < 0 )
8149+ Py_ssize_t i = PySequence_Size (trailers );
8150+ if (i < 0 )
8151+ return NULL ;
8152+ if (i > INT_MAX ) {
8153+ PyErr_SetString (PyExc_OverflowError ,
8154+ "sendfile() trailer is too large" );
81418155 return NULL ;
8156+ }
8157+ if (i > 0 ) {
8158+ sf .trl_cnt = (int )i ;
8159+ i = iov_setup (& (sf .trailers ), & tbuf ,
8160+ trailers , sf .trl_cnt , PyBUF_SIMPLE );
8161+ if (i < 0 )
8162+ return NULL ;
81428163#ifdef __APPLE__
8143- sbytes += i ;
8164+ sbytes += i ;
81448165#endif
8166+ }
81458167 }
81468168 }
81478169
@@ -8411,7 +8433,7 @@ static Py_ssize_t
84118433os_writev_impl (PyObject * module , int fd , PyObject * buffers )
84128434/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
84138435{
8414- int cnt ;
8436+ Py_ssize_t cnt ;
84158437 Py_ssize_t result ;
84168438 int async_err = 0 ;
84178439 struct iovec * iov ;
@@ -8423,6 +8445,8 @@ os_writev_impl(PyObject *module, int fd, PyObject *buffers)
84238445 return -1 ;
84248446 }
84258447 cnt = PySequence_Size (buffers );
8448+ if (cnt < 0 )
8449+ return -1 ;
84268450
84278451 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_SIMPLE ) < 0 ) {
84288452 return -1 ;
0 commit comments