@@ -6755,14 +6755,17 @@ static PyObject *
67556755os_setgroups (PyObject * module , PyObject * groups )
67566756/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
67576757{
6758- int i , len ;
6758+ Py_ssize_t i , len ;
67596759 gid_t grouplist [MAX_GROUPS ];
67606760
67616761 if (!PySequence_Check (groups )) {
67626762 PyErr_SetString (PyExc_TypeError , "setgroups argument must be a sequence" );
67636763 return NULL ;
67646764 }
67656765 len = PySequence_Size (groups );
6766+ if (len < 0 ) {
6767+ return NULL ;
6768+ }
67666769 if (len > MAX_GROUPS ) {
67676770 PyErr_SetString (PyExc_ValueError , "too many groups" );
67686771 return NULL ;
@@ -8076,9 +8079,9 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length)
80768079#if (defined(HAVE_SENDFILE ) && (defined(__FreeBSD__ ) || defined(__DragonFly__ ) \
80778080 || defined(__APPLE__ ))) || defined(HAVE_READV ) || defined(HAVE_WRITEV )
80788081static Py_ssize_t
8079- iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , int cnt , int type )
8082+ iov_setup (struct iovec * * iov , Py_buffer * * buf , PyObject * seq , Py_ssize_t cnt , int type )
80808083{
8081- int i , j ;
8084+ Py_ssize_t i , j ;
80828085 Py_ssize_t blen , total = 0 ;
80838086
80848087 * iov = PyMem_New (struct iovec , cnt );
@@ -8155,8 +8158,7 @@ static Py_ssize_t
81558158os_readv_impl (PyObject * module , int fd , PyObject * buffers )
81568159/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
81578160{
8158- int cnt ;
8159- Py_ssize_t n ;
8161+ Py_ssize_t cnt , n ;
81608162 int async_err = 0 ;
81618163 struct iovec * iov ;
81628164 Py_buffer * buf ;
@@ -8168,6 +8170,8 @@ os_readv_impl(PyObject *module, int fd, PyObject *buffers)
81688170 }
81698171
81708172 cnt = PySequence_Size (buffers );
8173+ if (cnt < 0 )
8174+ return -1 ;
81718175
81728176 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_WRITABLE ) < 0 )
81738177 return -1 ;
@@ -8310,15 +8314,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
83108314 "sendfile() headers must be a sequence" );
83118315 return NULL ;
83128316 } else {
8313- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8314- sf .hdr_cnt = PySequence_Size (headers );
8315- if (sf .hdr_cnt > 0 &&
8316- (i = iov_setup (& (sf .headers ), & hbuf ,
8317- headers , sf .hdr_cnt , PyBUF_SIMPLE )) < 0 )
8317+ Py_ssize_t i = PySequence_Size (headers );
8318+ if (i < 0 )
8319+ return NULL ;
8320+ if (i > INT_MAX ) {
8321+ PyErr_SetString (PyExc_OverflowError ,
8322+ "sendfile() header is too large" );
83188323 return NULL ;
8324+ }
8325+ if (i > 0 ) {
8326+ sf .hdr_cnt = (int )i ;
8327+ i = iov_setup (& (sf .headers ), & hbuf ,
8328+ headers , sf .hdr_cnt , PyBUF_SIMPLE );
8329+ if (i < 0 )
8330+ return NULL ;
83198331#ifdef __APPLE__
8320- sbytes += i ;
8332+ sbytes += i ;
83218333#endif
8334+ }
83228335 }
83238336 }
83248337 if (trailers != NULL) {
@@ -8327,15 +8340,24 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
83278340 "sendfile() trailers must be a sequence" );
83288341 return NULL ;
83298342 } else {
8330- Py_ssize_t i = 0 ; /* Avoid uninitialized warning */
8331- sf .trl_cnt = PySequence_Size (trailers );
8332- if (sf .trl_cnt > 0 &&
8333- (i = iov_setup (& (sf .trailers ), & tbuf ,
8334- trailers , sf .trl_cnt , PyBUF_SIMPLE )) < 0 )
8343+ Py_ssize_t i = PySequence_Size (trailers );
8344+ if (i < 0 )
8345+ return NULL ;
8346+ if (i > INT_MAX ) {
8347+ PyErr_SetString (PyExc_OverflowError ,
8348+ "sendfile() trailer is too large" );
83358349 return NULL ;
8350+ }
8351+ if (i > 0 ) {
8352+ sf .trl_cnt = (int )i ;
8353+ i = iov_setup (& (sf .trailers ), & tbuf ,
8354+ trailers , sf .trl_cnt , PyBUF_SIMPLE );
8355+ if (i < 0 )
8356+ return NULL ;
83368357#ifdef __APPLE__
8337- sbytes += i ;
8358+ sbytes += i ;
83388359#endif
8360+ }
83398361 }
83408362 }
83418363
@@ -8605,7 +8627,7 @@ static Py_ssize_t
86058627os_writev_impl (PyObject * module , int fd , PyObject * buffers )
86068628/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
86078629{
8608- int cnt ;
8630+ Py_ssize_t cnt ;
86098631 Py_ssize_t result ;
86108632 int async_err = 0 ;
86118633 struct iovec * iov ;
@@ -8617,6 +8639,8 @@ os_writev_impl(PyObject *module, int fd, PyObject *buffers)
86178639 return -1 ;
86188640 }
86198641 cnt = PySequence_Size (buffers );
8642+ if (cnt < 0 )
8643+ return -1 ;
86208644
86218645 if (iov_setup (& iov , & buf , buffers , cnt , PyBUF_SIMPLE ) < 0 ) {
86228646 return -1 ;
0 commit comments