gh-156173: Fix zlib.Decompress.flush() silently returning corrupted output instead of raising zlib.error - #156176
Conversation
…stead of raising `zlib.error`
|
I'm not sure about backporting, this is a behaviour change, although the old behaviour is incorrect. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
LGTM. 👍
If the result is not truncated, but corrupted, error is reasonable.
What do other decompressors do? There is a large difference between zlib and others, so it may be not easy to draw a right parallel.
|
They all error, testing >>> import bz2, lzma, zlib; from compression import zstd
>>> payload = b'abcdefgh' * 200
>>> corrupt = lambda blob, keep: blob[:keep] + bytes(60)
>>> d = zlib.decompressobj()
>>> d.decompress(corrupt(zlib.compress(payload, 9), 15), 1)
b'a'
>>> d.flush() # no error, but corrupt data.
b'bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdeP'
>>> zlib._ZlibDecompressor().decompress(corrupt(zlib.compress(payload, 9), 15))
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
zlib._ZlibDecompressor().decompress(corrupt(zlib.compress(payload, 9), 15))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
zlib.error: Error -3 while decompressing data: incorrect data check |
|
Backporting this is a good idea. If nothing else, this could alert users to silent data integrity failures. |
|
Thanks @StanFromIreland for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
GH-156270 is a backport of this pull request to the 3.15 branch. |
|
GH-156271 is a backport of this pull request to the 3.14 branch. |
|
GH-156272 is a backport of this pull request to the 3.13 branch. |
zlib.Decompress.flush()silently returns corrupted output instead of raisingzlib.error#156173