summaryrefslogtreecommitdiffstats
path: root/Modules/zlibmodule.c
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-11-04 23:40:00 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-11-04 23:40:00 (GMT)
commitec8c8ae7ae8553d108d09a29e530bf6f9a95a0c3 (patch)
tree48e33dc587d04087e763e9d80cf2de360dcb6e5f /Modules/zlibmodule.c
parent6caa6fb535e9a7d4a91d48e7806de04c5491b33c (diff)
parent519f43844babca8bb23cfc2cd0ba9f5ee0b26034 (diff)
downloadcpython-ec8c8ae7ae8553d108d09a29e530bf6f9a95a0c3.zip
cpython-ec8c8ae7ae8553d108d09a29e530bf6f9a95a0c3.tar.gz
cpython-ec8c8ae7ae8553d108d09a29e530bf6f9a95a0c3.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/zlibmodule.c')
-rw-r--r--Modules/zlibmodule.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index a53a5f0..3e9fdf4 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -693,12 +693,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;
}
self->eof = 1;
/* We will only get Z_BUF_ERROR if the output buffer was full