cpython: 850624632e9a (original) (raw)
--- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -186,6 +186,17 @@ class CompressTestCase(BaseCompressTestC 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:[](#l1.9)
self.skipTest("not enough free memory, need at least 4 GB")[](#l1.10)
data = b'x' * size[](#l1.11)
try:[](#l1.12)
self.assertRaises(OverflowError, zlib.compress, data, 1)[](#l1.13)
self.assertRaises(OverflowError, zlib.decompress, data)[](#l1.14)
finally:[](#l1.15)
data = None[](#l1.16)
+ class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase): # Test compression object
--- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -116,13 +116,20 @@ PyZlib_compress(PyObject *self, PyObject { PyObject *ReturnVal = NULL; Py_buffer pinput;
- Byte *input, *output = NULL;
- 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;
- if (pinput.len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,[](#l2.19)
"Size does not fit in an unsigned int");[](#l2.20)
goto error;[](#l2.21)
- } input = pinput.buf; length = pinput.len; @@ -130,10 +137,9 @@ PyZlib_compress(PyObject self, PyObject output = (Byte)malloc(zst.avail_out); if (output == NULL) {
PyBuffer_Release(&pinput);[](#l2.30) PyErr_SetString(PyExc_MemoryError,[](#l2.31) "Can't allocate memory to compress data");[](#l2.32)
return NULL;[](#l2.33)
} /* Past the point of no return. From here on out, we need to make sure @@ -196,10 +202,11 @@ PyDoc_STRVAR(decompress__doc__, static PyObject * PyZlib_decompress(PyObject *self, PyObject *args) {goto error;[](#l2.34)
- unsigned int length;
- int err; int wsize=DEF_WBITS; Py_ssize_t r_strlen=DEFAULTALLOC; z_stream zst; @@ -207,6 +214,12 @@ PyZlib_decompress(PyObject self, PyObje if (!PyArg_ParseTuple(args, "y|in:decompress", &pinput, &wsize, &r_strlen)) return NULL;
- if (pinput.len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,[](#l2.58)
"Size does not fit in an unsigned int");[](#l2.59)
goto error;[](#l2.60)
- } input = pinput.buf; length = pinput.len; @@ -216,10 +229,8 @@ PyZlib_decompress(PyObject *self, PyObje zst.avail_in = length; zst.avail_out = r_strlen;
- if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
PyBuffer_Release(&pinput);[](#l2.70)
return NULL;[](#l2.71)
- }
zst.zalloc = (alloc_func)NULL; zst.zfree = (free_func)Z_NULL;