]> granicus.if.org Git - python/commitdiff
Fix memory leaks in zlib.compress() and .decompress().
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 14 May 2011 21:07:36 +0000 (23:07 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 14 May 2011 21:07:36 +0000 (23:07 +0200)
Also, make sure that test_zlib tests decompress() for overly-large inputs.

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

index 852857bb41c7ad34f6d9d7a7e73b98a6d13240fa..48d9f582cf4379daf3c51f3acf9b658de0c99f22 100644 (file)
@@ -193,6 +193,7 @@ class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
         data = b'x' * size
         try:
             self.assertRaises(OverflowError, zlib.compress, data, 1)
+            self.assertRaises(OverflowError, zlib.decompress, data)
         finally:
             data = None
 
index 969db4aa4d1f777da656f26a340509cb243153a1..fa07739a16f8b0a7634185b084faf58251b6c2ee 100644 (file)
@@ -116,7 +116,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
 {
     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 @@ PyZlib_compress(PyObject *self, PyObject *args)
 
     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;
     }
-    length = pinput.len;
     input = pinput.buf;
+    length = pinput.len;
 
     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 @@ PyDoc_STRVAR(decompress__doc__,
 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 @@ PyZlib_decompress(PyObject *self, PyObject *args)
 
     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;
     }
-    length = pinput.len;
     input = pinput.buf;
+    length = pinput.len;
 
     if (r_strlen <= 0)
         r_strlen = 1;
@@ -230,10 +229,8 @@ PyZlib_decompress(PyObject *self, PyObject *args)
     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;