[Python-checkins] cpython (merge 3.2 -> default): Merge fix for memory leaks in zlib.compress() and .decompress().

nadeem.vawda python-checkins at python.org
Sat May 14 23:19:17 CEST 2011


http://hg.python.org/cpython/rev/9090970d5e17
changeset:   70110:9090970d5e17
parent:      70108:1893294872cd
parent:      70109:612781911887
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sat May 14 23:18:07 2011 +0200
summary:
  Merge fix for memory leaks in zlib.compress() and .decompress().

Also, make sure that test_zlib tests decompress() for overly-large inputs.

files:
  Lib/test/test_zlib.py |   1 +
  Modules/zlibmodule.c  |  25 +++++++++++--------------
  2 files changed, 12 insertions(+), 14 deletions(-)


diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -193,6 +193,7 @@
         data = b'x' * size
         try:
             self.assertRaises(OverflowError, zlib.compress, data, 1)
+            self.assertRaises(OverflowError, zlib.decompress, data)
         finally:
             data = None
 
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -116,7 +116,7 @@
 {
     PyObject *ReturnVal = NULL;
     Py_buffer pinput;
-    Byte *input, *output;
+    Byte *input, *output = NULL;
     unsigned int length;
     int level=Z_DEFAULT_COMPRESSION, err;
     z_stream zst;
@@ -127,20 +127,19 @@
 
     if (pinput.len > UINT_MAX) {
         PyErr_SetString(PyExc_OverflowError,
-            "size does not fit in an unsigned int");
-        return NULL;
+                        "Size does not fit in an unsigned int");
+        goto error;
     }
+    input = pinput.buf;
     length = pinput.len;
-    input = pinput.buf;
 
     zst.avail_out = length + length/1000 + 12 + 1;
 
     output = (Byte*)malloc(zst.avail_out);
     if (output == NULL) {
-        PyBuffer_Release(&pinput);
         PyErr_SetString(PyExc_MemoryError,
                         "Can't allocate memory to compress data");
-        return NULL;
+        goto error;
     }
 
     /* Past the point of no return.  From here on out, we need to make sure
@@ -203,7 +202,7 @@
 static PyObject *
 PyZlib_decompress(PyObject *self, PyObject *args)
 {
-    PyObject *result_str;
+    PyObject *result_str = NULL;
     Py_buffer pinput;
     Byte *input;
     unsigned int length;
@@ -218,11 +217,11 @@
 
     if (pinput.len > UINT_MAX) {
         PyErr_SetString(PyExc_OverflowError,
-            "size does not fit in an unsigned int");
-        return NULL;
+                        "Size does not fit in an unsigned int");
+        goto error;
     }
+    input = pinput.buf;
     length = pinput.len;
-    input = pinput.buf;
 
     if (r_strlen <= 0)
         r_strlen = 1;
@@ -230,10 +229,8 @@
     zst.avail_in = length;
     zst.avail_out = r_strlen;
 
-    if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
-        PyBuffer_Release(&pinput);
-        return NULL;
-    }
+    if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
+        goto error;
 
     zst.zalloc = (alloc_func)NULL;
     zst.zfree = (free_func)Z_NULL;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list