diff -r 76196691b5d0 Modules/zlibmodule.c --- a/Modules/zlibmodule.c Mon Jun 24 22:43:02 2013 -0700 +++ b/Modules/zlibmodule.c Tue Jun 25 14:16:33 2013 +0200 @@ -155,7 +155,7 @@ PyZlib_compress(PyObject *self, PyObject if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level)) return NULL; - if (pinput.len > UINT_MAX) { + if ((size_t)pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); goto error; @@ -238,28 +238,29 @@ PyZlib_decompress(PyObject *self, PyObje unsigned int length; int err; int wsize=DEF_WBITS; - Py_ssize_t r_strlen=DEFAULTALLOC; + unsigned int bufsize=DEFAULTALLOC, new_bufsize; z_stream zst; - if (!PyArg_ParseTuple(args, "y*|in:decompress", - &pinput, &wsize, &r_strlen)) + if (!PyArg_ParseTuple(args, "y*|iI:decompress", + &pinput, &wsize, &bufsize)) return NULL; - if (pinput.len > UINT_MAX) { + if ((size_t)pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); goto error; } + + if (bufsize == 0) + bufsize = 1; + input = pinput.buf; length = (unsigned int)pinput.len; - if (r_strlen <= 0) - r_strlen = 1; + zst.avail_in = length; + zst.avail_out = bufsize; - zst.avail_in = length; - zst.avail_out = r_strlen; - - if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) + if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize))) goto error; zst.zalloc = (alloc_func)NULL; @@ -303,14 +304,18 @@ PyZlib_decompress(PyObject *self, PyObje /* fall through */ case(Z_OK): /* need more memory */ - if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) { + if (bufsize <= (UINT_MAX >> 1)) + new_bufsize = bufsize << 1; + else + new_bufsize = UINT_MAX; + if (_PyBytes_Resize(&result_str, new_bufsize) < 0) { inflateEnd(&zst); goto error; } zst.next_out = - (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen; - zst.avail_out = r_strlen; - r_strlen = r_strlen << 1; + (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize; + zst.avail_out = bufsize; + bufsize = new_bufsize; break; default: inflateEnd(&zst); @@ -353,6 +358,12 @@ PyZlib_compressobj(PyObject *selfptr, Py &memLevel, &strategy, &zdict)) return NULL; + if ((size_t)zdict.len > UINT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "zdict length does not fit in an unsigned int"); + goto error; + } + self = newcompobject(&Comptype); if (self==NULL) goto error; @@ -367,7 +378,8 @@ PyZlib_compressobj(PyObject *selfptr, Py if (zdict.buf == NULL) { goto success; } else { - err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len); + err = deflateSetDictionary(&self->zst, + zdict.buf, (unsigned int)zdict.len); switch (err) { case (Z_OK): goto success; @@ -490,7 +502,7 @@ PyZlib_objcompress(compobject *self, PyO { int err; unsigned int inplen; - Py_ssize_t length = DEFAULTALLOC; + unsigned int length = DEFAULTALLOC, new_length; PyObject *RetVal = NULL; Py_buffer pinput; Byte *input; @@ -498,13 +510,13 @@ PyZlib_objcompress(compobject *self, PyO if (!PyArg_ParseTuple(args, "y*:compress", &pinput)) return NULL; - if (pinput.len > UINT_MAX) { + if ((size_t)pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); goto error_outer; } input = pinput.buf; - inplen = pinput.len; + inplen = (unsigned int)pinput.len; if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) goto error_outer; @@ -524,7 +536,11 @@ PyZlib_objcompress(compobject *self, PyO /* while Z_OK and the output buffer is full, there might be more output, so extend the output buffer and try again */ while (err == Z_OK && self->zst.avail_out == 0) { - if (_PyBytes_Resize(&RetVal, length << 1) < 0) { + if (length <= (UINT_MAX >> 1)) + new_length = length << 1; + else + new_length = UINT_MAX; + if (_PyBytes_Resize(&RetVal, new_length) < 0) { Py_DECREF(RetVal); RetVal = NULL; goto error; @@ -532,7 +548,7 @@ PyZlib_objcompress(compobject *self, PyO self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal) + length; self->zst.avail_out = length; - length = length << 1; + length = new_length; Py_BEGIN_ALLOW_THREADS err = deflate(&(self->zst), Z_NO_FLUSH); @@ -573,7 +589,7 @@ save_unconsumed_input(compobject *self, Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data); Py_ssize_t new_size; PyObject *new_data; - if ((Py_ssize_t)self->zst.avail_in > PY_SSIZE_T_MAX - old_size) { + if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) { PyErr_NoMemory(); return -1; } @@ -618,29 +634,20 @@ PyDoc_STRVAR(decomp_decompress__doc__, static PyObject * PyZlib_objdecompress(compobject *self, PyObject *args) { - int err, max_length = 0; - unsigned int inplen; - Py_ssize_t old_length, length = DEFAULTALLOC; + int err; + unsigned int max_length = 0; + unsigned int length = DEFAULTALLOC, old_length; PyObject *RetVal = NULL; Py_buffer pinput; - Byte *input; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput, - &max_length)) + if (!PyArg_ParseTuple(args, "y*|I:decompress", &pinput, &max_length)) return NULL; - if (pinput.len > UINT_MAX) { + if ((size_t)pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); goto error_outer; } - input = pinput.buf; - inplen = pinput.len; - if (max_length < 0) { - PyErr_SetString(PyExc_ValueError, - "max_length must be greater than zero"); - goto error_outer; - } /* limit amount of data allocated to max_length */ if (max_length && length > max_length) @@ -651,8 +658,8 @@ PyZlib_objdecompress(compobject *self, P ENTER_ZLIB(self); start_total_out = self->zst.total_out; - self->zst.avail_in = inplen; - self->zst.next_in = input; + self->zst.avail_in = (unsigned int)pinput.len; + self->zst.next_in = pinput.buf; self->zst.avail_out = length; self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal); @@ -667,7 +674,17 @@ PyZlib_objdecompress(compobject *self, P RetVal = NULL; goto error; } - err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len); + + if ((size_t)zdict_buf.len > UINT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "zdict length does not fit in an unsigned int"); + Py_DECREF(RetVal); + RetVal = NULL; + goto error; + } + + err = inflateSetDictionary(&(self->zst), + zdict_buf.buf, (unsigned int)zdict_buf.len); PyBuffer_Release(&zdict_buf); if (err != Z_OK) { zlib_error(self->zst, err, "while decompressing data"); @@ -755,7 +772,8 @@ PyDoc_STRVAR(comp_flush__doc__, static PyObject * PyZlib_flush(compobject *self, PyObject *args) { - int err, length = DEFAULTALLOC; + int err; + unsigned int length = DEFAULTALLOC, new_length; PyObject *RetVal; int flushmode = Z_FINISH; unsigned long start_total_out; @@ -786,7 +804,11 @@ PyZlib_flush(compobject *self, PyObject /* while Z_OK and the output buffer is full, there might be more output, so extend the output buffer and try again */ while (err == Z_OK && self->zst.avail_out == 0) { - if (_PyBytes_Resize(&RetVal, length << 1) < 0) { + if (length <= (UINT_MAX >> 1)) + new_length = length << 1; + else + new_length = UINT_MAX; + if (_PyBytes_Resize(&RetVal, new_length) < 0) { Py_DECREF(RetVal); RetVal = NULL; goto error; @@ -794,7 +816,7 @@ PyZlib_flush(compobject *self, PyObject self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal) + length; self->zst.avail_out = length; - length = length << 1; + length = new_length; Py_BEGIN_ALLOW_THREADS err = deflate(&(self->zst), flushmode); @@ -958,24 +980,25 @@ PyDoc_STRVAR(decomp_flush__doc__, static PyObject * PyZlib_unflush(compobject *self, PyObject *args) { - int err, length = DEFAULTALLOC; + int err; + unsigned int length = DEFAULTALLOC, new_length; PyObject * retval = NULL; unsigned long start_total_out; + Py_ssize_t size; - if (!PyArg_ParseTuple(args, "|i:flush", &length)) + if (!PyArg_ParseTuple(args, "|I:flush", &length)) return NULL; - if (length <= 0) { - PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); - return NULL; - } if (!(retval = PyBytes_FromStringAndSize(NULL, length))) return NULL; - ENTER_ZLIB(self); + size = PyBytes_GET_SIZE(self->unconsumed_tail); + start_total_out = self->zst.total_out; - self->zst.avail_in = PyBytes_GET_SIZE(self->unconsumed_tail); + /* save_unconsumed_input() ensures that unconsumed_tail length is lesser + or equal than UINT_MAX */ + self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int); self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail); self->zst.avail_out = length; self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval); @@ -987,14 +1010,18 @@ PyZlib_unflush(compobject *self, PyObjec /* while Z_OK and the output buffer is full, there might be more output, so extend the output buffer and try again */ while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) { - if (_PyBytes_Resize(&retval, length << 1) < 0) { + if (length <= (UINT_MAX >> 1)) + new_length = length << 1; + else + new_length = UINT_MAX; + if (_PyBytes_Resize(&retval, new_length) < 0) { Py_DECREF(retval); retval = NULL; goto error; } self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length; self->zst.avail_out = length; - length = length << 1; + length = new_length; Py_BEGIN_ALLOW_THREADS err = inflate(&(self->zst), Z_FINISH); @@ -1089,7 +1116,7 @@ PyZlib_adler32(PyObject *self, PyObject Py_BEGIN_ALLOW_THREADS /* Avoid truncation of length for very large buffers. adler32() takes length as an unsigned int, which may be narrower than Py_ssize_t. */ - while (len > (size_t) UINT_MAX) { + while ((size_t)len > UINT_MAX) { adler32val = adler32(adler32val, buf, UINT_MAX); buf += (size_t) UINT_MAX; len -= (size_t) UINT_MAX; @@ -1127,7 +1154,7 @@ PyZlib_crc32(PyObject *self, PyObject *a Py_BEGIN_ALLOW_THREADS /* Avoid truncation of length for very large buffers. crc32() takes length as an unsigned int, which may be narrower than Py_ssize_t. */ - while (len > (size_t) UINT_MAX) { + while ((size_t)len > UINT_MAX) { crc32val = crc32(crc32val, buf, UINT_MAX); buf += (size_t) UINT_MAX; len -= (size_t) UINT_MAX;