Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,20 @@ def test_decompress_eof_incomplete_stream(self):
dco.flush()
self.assertFalse(dco.eof)

def test_decompress_flush_corrupt_stream(self):
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
corrupt = x[:-1] + b'\x00'
dco = zlib.decompressobj()
self.assertEqual(dco.decompress(corrupt, 1), b'f')
self.assertRaises(zlib.error, dco.flush)

def test_decompress_flush_twice(self):
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
dco = zlib.decompressobj()
self.assertEqual(dco.decompress(x), b'foo')
self.assertEqual(dco.flush(), b'')
self.assertEqual(dco.flush(), b'')

def test_decompress_unused_data(self):
# Repeated calls to decompress() after EOF should accumulate data in
# dco.unused_data, instead of just storing the arg to the last call.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Calling :meth:`zlib.Decompress.flush` on invalid compressed data now
raises :exc:`zlib.error` instead of being silently ignored.
11 changes: 11 additions & 0 deletions Modules/zlibmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,13 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,

PyMutex_Lock(&self->mutex);

/* A previous flush() already reached the end of the stream and freed the
decompression state, so there is nothing left to process. */
if (!self->is_initialised) {
PyMutex_Unlock(&self->mutex);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
PyMutex_Unlock(&self->mutex);
return NULL;
Expand Down Expand Up @@ -1328,6 +1335,10 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
goto abort;
}
}
else if (err != Z_OK && err != Z_BUF_ERROR) {
zlib_error(state, self->zst, err, "while decompressing data");
goto abort;
}

return_value = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
if (return_value != NULL) {
Expand Down
Loading