Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b8a417

Browse files
committed
Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
1 parent 7619e88 commit 1b8a417

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

Lib/test/test_zlib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ def test_big_compress_buffer(self, size):
186186
def test_big_decompress_buffer(self, size):
187187
self.check_big_decompress_buffer(size, zlib.decompress)
188188

189+
@precisionbigmemtest(size=_4G + 100, memuse=1)
190+
def test_length_overflow(self, size):
191+
if size < _4G + 100:
192+
self.skipTest("not enough free memory, need at least 4 GB")
193+
data = b'x' * size
194+
try:
195+
self.assertRaises(OverflowError, zlib.compress, data, 1)
196+
self.assertRaises(OverflowError, zlib.decompress, data)
197+
finally:
198+
data = None
199+
189200

190201
class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
191202
# Test compression object

Modules/zlibmodule.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,30 @@ PyZlib_compress(PyObject *self, PyObject *args)
116116
{
117117
PyObject *ReturnVal = NULL;
118118
Py_buffer pinput;
119-
Byte *input, *output;
120-
int length, level=Z_DEFAULT_COMPRESSION, err;
119+
Byte *input, *output = NULL;
120+
unsigned int length;
121+
int level=Z_DEFAULT_COMPRESSION, err;
121122
z_stream zst;
122123

123124
/* require Python string object, optional 'level' arg */
124125
if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
125126
return NULL;
127+
128+
if (pinput.len > UINT_MAX) {
129+
PyErr_SetString(PyExc_OverflowError,
130+
"Size does not fit in an unsigned int");
131+
goto error;
132+
}
126133
input = pinput.buf;
127134
length = pinput.len;
128135

129136
zst.avail_out = length + length/1000 + 12 + 1;
130137

131138
output = (Byte*)malloc(zst.avail_out);
132139
if (output == NULL) {
133-
PyBuffer_Release(&pinput);
134140
PyErr_SetString(PyExc_MemoryError,
135141
"Can't allocate memory to compress data");
136-
return NULL;
142+
goto error;
137143
}
138144

139145
/* Past the point of no return. From here on out, we need to make sure
@@ -196,17 +202,24 @@ PyDoc_STRVAR(decompress__doc__,
196202
static PyObject *
197203
PyZlib_decompress(PyObject *self, PyObject *args)
198204
{
199-
PyObject *result_str;
205+
PyObject *result_str = NULL;
200206
Py_buffer pinput;
201207
Byte *input;
202-
int length, err;
208+
unsigned int length;
209+
int err;
203210
int wsize=DEF_WBITS;
204211
Py_ssize_t r_strlen=DEFAULTALLOC;
205212
z_stream zst;
206213

207214
if (!PyArg_ParseTuple(args, "y*|in:decompress",
208215
&pinput, &wsize, &r_strlen))
209216
return NULL;
217+
218+
if (pinput.len > UINT_MAX) {
219+
PyErr_SetString(PyExc_OverflowError,
220+
"Size does not fit in an unsigned int");
221+
goto error;
222+
}
210223
input = pinput.buf;
211224
length = pinput.len;
212225

@@ -216,10 +229,8 @@ PyZlib_decompress(PyObject *self, PyObject *args)
216229
zst.avail_in = length;
217230
zst.avail_out = r_strlen;
218231

219-
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
220-
PyBuffer_Release(&pinput);
221-
return NULL;
222-
}
232+
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
233+
goto error;
223234

224235
zst.zalloc = (alloc_func)NULL;
225236
zst.zfree = (free_func)Z_NULL;

0 commit comments

Comments
 (0)