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_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ def test_decompressor_after_eof(self):
lzd.decompress(COMPRESSED_XZ)
self.assertRaises(EOFError, lzd.decompress, b"nyan")

def test_decompressor_empty_max_length_zero_repeated(self):
# gh-160000: repeated decompress(b'', max_length=0) calls must
# return b'' instead of raising LZMAError on the second call.
lzd = LZMADecompressor()
for _ in range(3):
self.assertEqual(lzd.decompress(b'', max_length=0), b'')
# Same after real input: one byte out, then empty calls,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be split into separate tests or parameterized in some way.

# then the rest of the stream still decompresses.
lzd = LZMADecompressor()
self.assertEqual(lzd.decompress(COMPRESSED_XZ, 1), INPUT[:1])
for _ in range(3):
self.assertEqual(lzd.decompress(b'', max_length=0), b'')
self.assertEqual(lzd.decompress(b''), INPUT[1:])

def test_decompressor_memlimit(self):
lzd = LZMADecompressor(memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`lzma.LZMADecompressor` raising :exc:`LZMAError` on repeated
``decompress(b'', max_length=0)`` calls.
Comment on lines +1 to +2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix :class:`lzma.LZMADecompressor` raising :exc:`LZMAError` on repeated
``decompress(b'', max_length=0)`` calls.
Fix :class:`lzma.LZMADecompressor` raising :exc:`LZMAError` on repeated
``decompress()`` calls with ``max_length=0``.

3 changes: 2 additions & 1 deletion Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,8 @@ decompress_buf(Decompressor *d, Py_ssize_t max_length)
lzret = lzma_code(lzs, LZMA_RUN);
Py_END_ALLOW_THREADS

if (lzret == LZMA_BUF_ERROR && lzs->avail_in == 0 && lzs->avail_out > 0) {
if (lzret == LZMA_BUF_ERROR &&
(lzs->avail_in == 0 || lzs->avail_out == 0)) {
lzret = LZMA_OK; /* That wasn't a real error */
Comment on lines +962 to 964

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than ignoring the error, we should add the input bytes to the input buffer, then return empty bytes without calling the library.

}
if (catch_lzma_error(state, lzret)) {
Expand Down
Loading