]> granicus.if.org Git - python/commitdiff
Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to...
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 4 Nov 2012 23:55:06 +0000 (00:55 +0100)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 4 Nov 2012 23:55:06 +0000 (00:55 +0100)
Patch by Serhiy Storchaka.

Lib/test/test_zlib.py
Misc/NEWS
Modules/zlibmodule.c

index 7f631437b59793cb81296b20e49e2e2141ee0cbb..eb3dd6e1ec440f45cd222795846f30a435636976 100644 (file)
@@ -426,6 +426,19 @@ class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
             c.flush()
             self.assertRaises(ValueError, c.copy)
 
+    def test_decompress_unused_data(self):
+        # Repeated calls to decompress() after EOF should accumulate data in
+        # dco.unused_data, instead of just storing the arg to the last call.
+        x = zlib.compress(HAMLET_SCENE) + HAMLET_SCENE
+        for step in 1, 2, 100:
+            dco = zlib.decompressobj()
+            data = b''.join(dco.decompress(x[i : i + step])
+                            for i in range(0, len(x), step))
+            data += dco.flush()
+
+            self.assertEqual(data, HAMLET_SCENE)
+            self.assertEqual(dco.unused_data, HAMLET_SCENE)
+
     if hasattr(zlib.decompressobj(), "copy"):
         def test_decompresscopy(self):
             # Test copying a decompression object
index 883bd93c063b736938bee7649c81df32846f660a..b2ad14b50d387ad6bcc4168409f49b98a004a7c5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #16350: zlib.Decompress.decompress() now accumulates data from
+  successive calls after EOF in unused_data, instead of only saving the argument
+  to the last call. Patch by Serhiy Storchaka.
+
 - Issue #12759: sre_parse now raises a proper error when the name of the group
   is missing.  Initial patch by Serhiy Storchaka.
 
index 035aa8e085da66b05ff5ca813a87be8be3117ad9..95e85b5aa1cc5ca183574c55e3f2ca951027caad 100644 (file)
@@ -566,12 +566,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 = PyString_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 = PyString_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 = PyString_FromStringAndSize(NULL, new_size);
+            if (new_data == NULL) {
+                Py_DECREF(RetVal);
+                RetVal = NULL;
+                goto error;
+            }
+            Py_MEMCPY(PyString_AS_STRING(new_data),
+                      PyString_AS_STRING(self->unused_data), old_size);
+            Py_MEMCPY(PyString_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