@@ -116,24 +116,30 @@ PyZlib_compress(PyObject *self, PyObject *args)
116116{
117117 PyObject * ReturnVal = NULL ;
118118 Py_buffer pinput ;
119- Byte * input , * output ;
120- int length , level = Z_DEFAULT_COMPRESSION , err ;
119+ Byte * input , * output = NULL ;
120+ unsigned int length ;
121+ int level = Z_DEFAULT_COMPRESSION , err ;
121122 z_stream zst ;
122123
123124 /* require Python string object, optional 'level' arg */
124125 if (!PyArg_ParseTuple (args , "y*|i:compress" , & pinput , & level ))
125126 return NULL ;
127+
128+ if (pinput .len > UINT_MAX ) {
129+ PyErr_SetString (PyExc_OverflowError ,
130+ "Size does not fit in an unsigned int" );
131+ goto error ;
132+ }
126133 input = pinput .buf ;
127134 length = pinput .len ;
128135
129136 zst .avail_out = length + length /1000 + 12 + 1 ;
130137
131138 output = (Byte * )malloc (zst .avail_out );
132139 if (output == NULL ) {
133- PyBuffer_Release (& pinput );
134140 PyErr_SetString (PyExc_MemoryError ,
135141 "Can't allocate memory to compress data" );
136- return NULL ;
142+ goto error ;
137143 }
138144
139145 /* Past the point of no return. From here on out, we need to make sure
@@ -196,17 +202,24 @@ PyDoc_STRVAR(decompress__doc__,
196202static PyObject *
197203PyZlib_decompress (PyObject * self , PyObject * args )
198204{
199- PyObject * result_str ;
205+ PyObject * result_str = NULL ;
200206 Py_buffer pinput ;
201207 Byte * input ;
202- int length , err ;
208+ unsigned int length ;
209+ int err ;
203210 int wsize = DEF_WBITS ;
204211 Py_ssize_t r_strlen = DEFAULTALLOC ;
205212 z_stream zst ;
206213
207214 if (!PyArg_ParseTuple (args , "y*|in:decompress" ,
208215 & pinput , & wsize , & r_strlen ))
209216 return NULL ;
217+
218+ if (pinput .len > UINT_MAX ) {
219+ PyErr_SetString (PyExc_OverflowError ,
220+ "Size does not fit in an unsigned int" );
221+ goto error ;
222+ }
210223 input = pinput .buf ;
211224 length = pinput .len ;
212225
@@ -216,10 +229,8 @@ PyZlib_decompress(PyObject *self, PyObject *args)
216229 zst .avail_in = length ;
217230 zst .avail_out = r_strlen ;
218231
219- if (!(result_str = PyBytes_FromStringAndSize (NULL , r_strlen ))) {
220- PyBuffer_Release (& pinput );
221- return NULL ;
222- }
232+ if (!(result_str = PyBytes_FromStringAndSize (NULL , r_strlen )))
233+ goto error ;
223234
224235 zst .zalloc = (alloc_func )NULL ;
225236 zst .zfree = (free_func )Z_NULL ;
0 commit comments