Skip to content

gh-160000: Fix LZMADecompressor raising on repeated empty decompress calls - #160205

Open
bunnysayzz wants to merge 1 commit into
python:mainfrom
bunnysayzz:gh-160000-lzma-empty-decompress
Open

bunnysayzz wants to merge 1 commit into
python:mainfrom
bunnysayzz:gh-160000-lzma-empty-decompress

Conversation

@bunnysayzz

@bunnysayzz bunnysayzz commented Oct 22, 2026 •

Copy link
Copy Markdown

Fixes #160000.

LZMADecompressor.decompress(b'', max_length=0) works once, then raises LZMAError: Insufficient buffer space on the second call. Same after real input when output is pending internally.

In Modules/_lzmamodule.c, decompress_buf treats LZMA_BUF_ERROR from liblzma as benign only when avail_in == 0 and avail_out > 0. With max_length=0 there is no output space, so that check never fires for the empty call, and the error escapes. The fix treats it as benign when avail_in == 0 or avail_out == 0. That is safe because the avail_out == 0 branch either breaks once the requested length is reached or grows the buffer, so it cannot spin.

Also adds a regression test in Lib/test/test_lzma.py covering the empty case and the after-real-input case, and a NEWS entry.

Verified by building CPython from main and running the test suite: test_lzma passes (125 tests), the issue repro returns b'' on repeated calls, the decompressor stays usable afterwards, and corrupt input still raises LZMAError.

Note: this is my first CPython PR, I will complete the CLA check when prompted.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Comment thread Lib/test/test_lzma.py
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.

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

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.

Comment on lines +1 to +2
Fix :class:`lzma.LZMADecompressor` raising :exc:`LZMAError` on repeated
``decompress(b'', max_length=0)`` 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.

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``.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LZMADecompressor.decompress(b'', max_length=0) raises LZMAError on the second consecutive call

3 participants