Skip to content

Commit d43200c

Browse files
committed
gh-160000: Fix LZMADecompressor raising on repeated empty decompress calls
1 parent 5a63d35 commit d43200c

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎Lib/test/test_lzma.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ def test_decompressor_after_eof(self):
7878
lzd.decompress(COMPRESSED_XZ)
7979
self.assertRaises(EOFError, lzd.decompress, b"nyan")
8080

81+
def test_decompressor_empty_max_length_zero_repeated(self):
82+
# gh-160000: repeated decompress(b'', max_length=0) calls must
83+
# return b'' instead of raising LZMAError on the second call.
84+
lzd = LZMADecompressor()
85+
for _ in range(3):
86+
self.assertEqual(lzd.decompress(b'', max_length=0), b'')
87+
# Same after real input: one byte out, then empty calls,
88+
# then the rest of the stream still decompresses.
89+
lzd = LZMADecompressor()
90+
self.assertEqual(lzd.decompress(COMPRESSED_XZ, 1), INPUT[:1])
91+
for _ in range(3):
92+
self.assertEqual(lzd.decompress(b'', max_length=0), b'')
93+
self.assertEqual(lzd.decompress(b''), INPUT[1:])
94+
8195
def test_decompressor_memlimit(self):
8296
lzd = LZMADecompressor(memlimit=1024)
8397
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`lzma.LZMADecompressor` raising :exc:`LZMAError` on repeated
2+
``decompress(b'', max_length=0)`` calls.

‎Modules/_lzmamodule.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,8 @@ decompress_buf(Decompressor *d, Py_ssize_t max_length)
959959
lzret = lzma_code(lzs, LZMA_RUN);
960960
Py_END_ALLOW_THREADS
961961

962-
if (lzret == LZMA_BUF_ERROR && lzs->avail_in == 0 && lzs->avail_out > 0) {
962+
if (lzret == LZMA_BUF_ERROR &&
963+
(lzs->avail_in == 0 || lzs->avail_out == 0)) {
963964
lzret = LZMA_OK; /* That wasn't a real error */
964965
}
965966
if (catch_lzma_error(state, lzret)) {

0 commit comments

Comments
 (0)