@@ -420,22 +420,26 @@ PyDoc_STRVAR(comp_compress__doc__,
420420static PyObject *
421421PyZlib_objcompress (compobject * self , PyObject * args )
422422{
423- int err , inplen ;
423+ int err ;
424+ unsigned int inplen ;
424425 Py_ssize_t length = DEFAULTALLOC ;
425- PyObject * RetVal ;
426+ PyObject * RetVal = NULL ;
426427 Py_buffer pinput ;
427428 Byte * input ;
428429 unsigned long start_total_out ;
429430
430431 if (!PyArg_ParseTuple (args , "y*:compress" , & pinput ))
431432 return NULL ;
433+ if (pinput .len > UINT_MAX ) {
434+ PyErr_SetString (PyExc_OverflowError ,
435+ "Size does not fit in an unsigned int" );
436+ goto error_outer ;
437+ }
432438 input = pinput .buf ;
433439 inplen = pinput .len ;
434440
435- if (!(RetVal = PyBytes_FromStringAndSize (NULL , length ))) {
436- PyBuffer_Release (& pinput );
437- return NULL ;
438- }
441+ if (!(RetVal = PyBytes_FromStringAndSize (NULL , length )))
442+ goto error_outer ;
439443
440444 ENTER_ZLIB (self );
441445
@@ -484,6 +488,7 @@ PyZlib_objcompress(compobject *self, PyObject *args)
484488
485489 error :
486490 LEAVE_ZLIB (self );
491+ error_outer :
487492 PyBuffer_Release (& pinput );
488493 return RetVal ;
489494}
@@ -502,32 +507,35 @@ PyDoc_STRVAR(decomp_decompress__doc__,
502507static PyObject *
503508PyZlib_objdecompress (compobject * self , PyObject * args )
504509{
505- int err , inplen , max_length = 0 ;
510+ int err , max_length = 0 ;
511+ unsigned int inplen ;
506512 Py_ssize_t old_length , length = DEFAULTALLOC ;
507- PyObject * RetVal ;
513+ PyObject * RetVal = NULL ;
508514 Py_buffer pinput ;
509515 Byte * input ;
510516 unsigned long start_total_out ;
511517
512518 if (!PyArg_ParseTuple (args , "y*|i:decompress" , & pinput ,
513519 & max_length ))
514520 return NULL ;
521+ if (pinput .len > UINT_MAX ) {
522+ PyErr_SetString (PyExc_OverflowError ,
523+ "Size does not fit in an unsigned int" );
524+ goto error_outer ;
525+ }
515526 input = pinput .buf ;
516527 inplen = pinput .len ;
517528 if (max_length < 0 ) {
518- PyBuffer_Release (& pinput );
519529 PyErr_SetString (PyExc_ValueError ,
520530 "max_length must be greater than zero" );
521- return NULL ;
531+ goto error_outer ;
522532 }
523533
524534 /* limit amount of data allocated to max_length */
525535 if (max_length && length > max_length )
526536 length = max_length ;
527- if (!(RetVal = PyBytes_FromStringAndSize (NULL , length ))) {
528- PyBuffer_Release (& pinput );
529- return NULL ;
530- }
537+ if (!(RetVal = PyBytes_FromStringAndSize (NULL , length )))
538+ goto error_outer ;
531539
532540 ENTER_ZLIB (self );
533541
@@ -621,6 +629,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
621629
622630 error :
623631 LEAVE_ZLIB (self );
632+ error_outer :
624633 PyBuffer_Release (& pinput );
625634 return RetVal ;
626635}
0 commit comments