diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-11-04 23:37:42 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-11-04 23:37:42 (GMT) |
commit | 39079946a257522ecb66cb068c872f5d1fe70ea3 (patch) | |
tree | 4c5fe8fa06bde62f9d7cfff518d8c8b23040ac38 /Modules | |
parent | 6c5f5210be9f68252b72d7b5e8dc2bde20d90c9d (diff) | |
download | cpython-39079946a257522ecb66cb068c872f5d1fe70ea3.zip cpython-39079946a257522ecb66cb068c872f5d1fe70ea3.tar.gz cpython-39079946a257522ecb66cb068c872f5d1fe70ea3.tar.bz2 |
Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Patch by Serhiy Storchaka.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/zlibmodule.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index a1e605b..7734ba6 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -610,12 +610,29 @@ PyZlib_objdecompress(compobject *self, PyObject *args) preserved. */ if (err == Z_STREAM_END) { - Py_XDECREF(self->unused_data); /* Free original empty string */ - self->unused_data = PyBytes_FromStringAndSize( - (char *)self->zst.next_in, self->zst.avail_in); - if (self->unused_data == NULL) { - Py_DECREF(RetVal); - goto error; + if (self->zst.avail_in > 0) { + /* Append the leftover data to the existing value of unused_data. */ + Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data); + Py_ssize_t new_size = old_size + self->zst.avail_in; + PyObject *new_data; + if (new_size <= old_size) { /* Check for overflow. */ + PyErr_NoMemory(); + Py_DECREF(RetVal); + RetVal = NULL; + goto error; + } + new_data = PyBytes_FromStringAndSize(NULL, new_size); + if (new_data == NULL) { + Py_DECREF(RetVal); + RetVal = NULL; + goto error; + } + Py_MEMCPY(PyBytes_AS_STRING(new_data), + PyBytes_AS_STRING(self->unused_data), old_size); + Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size, + self->zst.next_in, self->zst.avail_in); + Py_DECREF(self->unused_data); + self->unused_data = new_data; } /* We will only get Z_BUF_ERROR if the output buffer was full but there wasn't more output when we tried again, so it is |