@@ -6642,14 +6642,17 @@ static PyObject *
66426642os_setgroups (PyObject * module , PyObject * groups )
66436643/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
66446644{
6645- int i , len ;
6645+ Py_ssize_t i , len ;
66466646 gid_t grouplist [MAX_GROUPS ];
66476647
66486648 if (!PySequence_Check (groups )) {
66496649 PyErr_SetString (PyExc_TypeError , "setgroups argument must be a sequence" );
66506650 return NULL ;
66516651 }
66526652 len = PySequence_Size (groups );
6653+ if (len < 0 ) {
6654+ return NULL ;
6655+ }
66536656 if (len > MAX_GROUPS ) {
66546657 PyErr_SetString (PyExc_ValueError , "too many groups" );
66556658 return NULL ;
@@ -7877,9 +7880,9 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length)
78777880#if (defined(HAVE_SENDFILE ) && (defined(__FreeBSD__ ) || defined(__DragonFly__ ) \
78787881 || defined(__APPLE__ ))) || defined(HAVE_READV ) || defined(HAVE_WRITEV )
78797882static Py_ssize_t
7880- iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , int cnt , int type )
7883+ iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , Py_ssize_t cnt , int type )
78817884{
7882- int i , j ;
7885+ Py_ssize_t i , j ;
78837886 Py_ssize_t blen , total = 0 ;
78847887
78857888 * iov = PyMem_New (struct iovec , cnt );
@@ -7956,8 +7959,7 @@ static Py_ssize_t
79567959os_readv_impl (PyObject * module , int fd , PyObject * buffers )
79577960/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
79587961{
7959- int cnt ;
7960- Py_ssize_t n ;
7962+ Py_ssize_t cnt , n ;
79617963 int async_err = 0 ;
79627964 struct iovec * iov ;
79637965 Py_buffer * buf ;
@@ -7969,6 +7971,8 @@ os_readv_impl(PyObject *module, int fd, PyObject *buffers)
79697971 }
79707972
79717973 cnt = PySequence_Size (buffers );
7974+ if (cnt < 0 )
7975+ return -1 ;
79727976
79737977 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_WRITABLE ) < 0 )
79747978 return -1 ;
@@ -8107,15 +8111,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
81078111 "sendfile() headers must be a sequence" );
81088112 return NULL ;
81098113 } else {
8110- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8111- sf .hdr_cnt = PySequence_Size (headers );
8112- if (sf .hdr_cnt > 0 &&
8113- (i = iov_setup (& (sf .headers ), & hbuf ,
8114- headers , sf .hdr_cnt , PyBUF_SIMPLE )) < 0 )
8114+ Py_ssize_t i = PySequence_Size (headers );
8115+ if (i < 0 )
8116+ return NULL ;
8117+ if (i > INT_MAX ) {
8118+ PyErr_SetString (PyExc_OverflowError ,
8119+ "sendfile() header is too large" );
81158120 return NULL ;
8121+ }
8122+ if (i > 0 ) {
8123+ sf .hdr_cnt = (int )i ;
8124+ i = iov_setup (& (sf .headers ), & hbuf ,
8125+ headers , sf .hdr_cnt , PyBUF_SIMPLE );
8126+ if (i < 0 )
8127+ return NULL ;
81168128#ifdef __APPLE__
8117- sbytes += i ;
8129+ sbytes += i ;
81188130#endif
8131+ }
81198132 }
81208133 }
81218134 if (trailers != NULL) {
@@ -8124,15 +8137,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
81248137 "sendfile() trailers must be a sequence" );
81258138 return NULL ;
81268139 } else {
8127- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8128- sf .trl_cnt = PySequence_Size (trailers );
8129- if (sf .trl_cnt > 0 &&
8130- (i = iov_setup (& (sf .trailers ), & tbuf ,
8131- trailers , sf .trl_cnt , PyBUF_SIMPLE )) < 0 )
8140+ Py_ssize_t i = PySequence_Size (trailers );
8141+ if (i < 0 )
8142+ return NULL ;
8143+ if (i > INT_MAX ) {
8144+ PyErr_SetString (PyExc_OverflowError ,
8145+ "sendfile() trailer is too large" );
81328146 return NULL ;
8147+ }
8148+ if (i > 0 ) {
8149+ sf .trl_cnt = (int )i ;
8150+ i = iov_setup (& (sf .trailers ), & tbuf ,
8151+ trailers , sf .trl_cnt , PyBUF_SIMPLE );
8152+ if (i < 0 )
8153+ return NULL ;
81338154#ifdef __APPLE__
8134- sbytes += i ;
8155+ sbytes += i ;
81358156#endif
8157+ }
81368158 }
81378159 }
81388160
@@ -8402,7 +8424,7 @@ static Py_ssize_t
84028424os_writev_impl (PyObject * module , int fd , PyObject * buffers )
84038425/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
84048426{
8405- int cnt ;
8427+ Py_ssize_t cnt ;
84068428 Py_ssize_t result ;
84078429 int async_err = 0 ;
84088430 struct iovec * iov ;
@@ -8414,6 +8436,8 @@ os_writev_impl(PyObject *module, int fd, PyObject *buffers)
84148436 return -1 ;
84158437 }
84168438 cnt = PySequence_Size (buffers );
8439+ if (cnt < 0 )
8440+ return -1 ;
84178441
84188442 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_SIMPLE ) < 0 ) {
84198443 return -1 ;
0 commit comments