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

Skip to content

Commit 154bdf9

Browse files
committed
Fix memory leaks in zlib.compress() and .decompress().
Also, make sure that test_zlib tests decompress() for overly-large inputs.
1 parent d8eab60 commit 154bdf9

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

Lib/test/test_zlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def test_length_overflow(self, size):
193193
data = b'x' * size
194194
try:
195195
self.assertRaises(OverflowError, zlib.compress, data, 1)
196+
self.assertRaises(OverflowError, zlib.decompress, data)
196197
finally:
197198
data = None
198199

Modules/zlibmodule.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
116116
{
117117
PyObject *ReturnVal = NULL;
118118
Py_buffer pinput;
119-
Byte *input, *output;
119+
Byte *input, *output = NULL;
120120
unsigned int length;
121121
int level=Z_DEFAULT_COMPRESSION, err;
122122
z_stream zst;
@@ -127,20 +127,19 @@ PyZlib_compress(PyObject *self, PyObject *args)
127127

128128
if (pinput.len > UINT_MAX) {
129129
PyErr_SetString(PyExc_OverflowError,
130-
"size does not fit in an unsigned int");
131-
return NULL;
130+
"Size does not fit in an unsigned int");
131+
goto error;
132132
}
133-
length = pinput.len;
134133
input = pinput.buf;
134+
length = pinput.len;
135135

136136
zst.avail_out = length + length/1000 + 12 + 1;
137137

138138
output = (Byte*)malloc(zst.avail_out);
139139
if (output == NULL) {
140-
PyBuffer_Release(&pinput);
141140
PyErr_SetString(PyExc_MemoryError,
142141
"Can't allocate memory to compress data");
143-
return NULL;
142+
goto error;
144143
}
145144

146145
/* Past the point of no return. From here on out, we need to make sure
@@ -203,7 +202,7 @@ PyDoc_STRVAR(decompress__doc__,
203202
static PyObject *
204203
PyZlib_decompress(PyObject *self, PyObject *args)
205204
{
206-
PyObject *result_str;
205+
PyObject *result_str = NULL;
207206
Py_buffer pinput;
208207
Byte *input;
209208
unsigned int length;
@@ -218,22 +217,20 @@ PyZlib_decompress(PyObject *self, PyObject *args)
218217

219218
if (pinput.len > UINT_MAX) {
220219
PyErr_SetString(PyExc_OverflowError,
221-
"size does not fit in an unsigned int");
222-
return NULL;
220+
"Size does not fit in an unsigned int");
221+
goto error;
223222
}
224-
length = pinput.len;
225223
input = pinput.buf;
224+
length = pinput.len;
226225

227226
if (r_strlen <= 0)
228227
r_strlen = 1;
229228

230229
zst.avail_in = length;
231230
zst.avail_out = r_strlen;
232231

233-
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
234-
PyBuffer_Release(&pinput);
235-
return NULL;
236-
}
232+
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
233+
goto error;
237234

238235
zst.zalloc = (alloc_func)NULL;
239236
zst.zfree = (free_func)Z_NULL;

0 commit comments

Comments
 (0)