]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #8650: zlib.compress() and zlib.decompress() raise an OverflowError if
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 4 Jan 2011 02:07:36 +0000 (02:07 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 4 Jan 2011 02:07:36 +0000 (02:07 +0000)
the input buffer length doesn't fit into an unsigned int (length bigger than
2^32-1 bytes).

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

index 0f5a1cac88b47424684b4cc41956be2055cb16e8..5615c2d23967cf2e88b4e9c1a77e31b9a544f780 100644 (file)
@@ -2,7 +2,7 @@ import unittest
 from test import support
 import binascii
 import random
-from test.support import precisionbigmemtest, _1G
+from test.support import precisionbigmemtest, _1G, _4G
 
 zlib = support.import_module('zlib')
 
@@ -158,6 +158,16 @@ class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
     def test_big_decompress_buffer(self, size):
         self.check_big_decompress_buffer(size, zlib.decompress)
 
+    @precisionbigmemtest(size=_4G + 100, memuse=1)
+    def test_length_overflow(self, size):
+        if size < _4G + 100:
+            self.skipTest("not enough free memory, need at least 4 GB")
+        data = b'x' * size
+        try:
+            self.assertRaises(OverflowError, zlib.compress, data, 1)
+        finally:
+            data = None
+
 
 class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
     # Test compression object
index d7610b3db72fbd18bcc56b9d5e7a346652f235e7..1b77950249b9196dd8a0cecf408f329ef4d46bbb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #8650: zlib.compress() and zlib.decompress() raise an OverflowError if
+  the input buffer length doesn't fit into an unsigned int (length bigger than
+  2^32-1 bytes).
+
 - Issue #6643: Reinitialize locks held within the threading module after fork
   to avoid a potential rare deadlock or crash on some platforms.
 
index 54ab9a149959ff12a19d46529931a281c70c362e..a03045ed169929caf45fc699f85f1ffc94ad6760 100644 (file)
@@ -117,14 +117,21 @@ PyZlib_compress(PyObject *self, PyObject *args)
     PyObject *ReturnVal = NULL;
     Py_buffer pinput;
     Byte *input, *output;
-    int length, level=Z_DEFAULT_COMPRESSION, err;
+    unsigned int length;
+    int level=Z_DEFAULT_COMPRESSION, err;
     z_stream zst;
 
     /* require Python string object, optional 'level' arg */
     if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
         return NULL;
-    input = pinput.buf;
+
+    if (pinput.len > UINT_MAX) {
+        PyErr_SetString(PyExc_OverflowError,
+            "size does not fit in an unsigned int");
+        return NULL;
+    }
     length = pinput.len;
+    input = pinput.buf;
 
     zst.avail_out = length + length/1000 + 12 + 1;
 
@@ -199,7 +206,8 @@ PyZlib_decompress(PyObject *self, PyObject *args)
     PyObject *result_str;
     Py_buffer pinput;
     Byte *input;
-    int length, err;
+    unsigned int length;
+    int err;
     int wsize=DEF_WBITS;
     Py_ssize_t r_strlen=DEFAULTALLOC;
     z_stream zst;
@@ -207,8 +215,14 @@ PyZlib_decompress(PyObject *self, PyObject *args)
     if (!PyArg_ParseTuple(args, "y*|in:decompress",
                           &pinput, &wsize, &r_strlen))
         return NULL;
-    input = pinput.buf;
+
+    if (pinput.len > UINT_MAX) {
+        PyErr_SetString(PyExc_OverflowError,
+            "size does not fit in an unsigned int");
+        return NULL;
+    }
     length = pinput.len;
+    input = pinput.buf;
 
     if (r_strlen <= 0)
         r_strlen = 1;