Skip to content

[3.12] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362) - #156739

Merged
Yhg1s merged 5 commits into
python:3.12from
miss-islington:backport-1b424c0-3.12
Sep 30, 2026
Merged

Yhg1s merged 5 commits into
python:3.12from
miss-islington:backport-1b424c0-3.12

Conversation

@miss-islington

@miss-islington miss-islington commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

Co-authored-by: Petr Viktorin encukou@gmail.com
Co-authored-by: tonghuaroot tonghuaroot@gmail.com

…tandard (pythonGH-156003) (pythonGH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
@encukou
encukou marked this pull request as draft September 10, 2026 13:46
…ssors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
@encukou
encukou marked this pull request as ready for review September 16, 2026 16:03
@encukou

encukou commented Sep 16, 2026

Copy link
Copy Markdown
Member

This now includes the fix from #157180.

@Yhg1s
Yhg1s merged commit 09a2e7e into python:3.12 Sep 30, 2026
27 checks passed
@miss-islington
miss-islington deleted the backport-1b424c0-3.12 branch September 30, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants