From 772c47ee162f783fb3a0873e06848c0cafe307b1 Mon Sep 17 00:00:00 2001 From: Aathithya Arasu S <142731071+Aathi-27@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:15:26 +0530 Subject: [PATCH 1/4] gh-162153: Fix zlib._ZlibDecompressor dropping input over 4 GiB when max_length is reached --- Modules/zlibmodule.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 31533fa047af0b6..658b973693d020b 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -1541,6 +1541,13 @@ decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length) } } } while (self->zst.avail_out == 0); + /* Stop when the output limit is reached: no more input is needed, + and re-running arrange_input_buffer() would overwrite + zst.avail_in without crediting its unconsumed remainder to + avail_in_real, silently dropping that input (see gh-162153). */ + if (obuflen == -2) { + break; + } } while(err != Z_STREAM_END && self->avail_in_real != 0); if (err == Z_STREAM_END) { From 0c3c398afb428b002b9d162c6a7f1887743aa799 Mon Sep 17 00:00:00 2001 From: Aathithya Arasu S <142731071+Aathi-27@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:16:28 +0530 Subject: [PATCH 2/4] gh-162153: Add bigmem regression test for decompress() with max_length and >4 GiB input --- Lib/test/test_zlib.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 3243448043b5c21..d0450dd4074e5fb 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -1195,6 +1195,35 @@ def testDecompress4G(self, size): compressed = None decompressed = None + @support.skip_if_pgo_task + @bigmemtest(size=_4G + 100, memuse=2.5) + def testDecompressMaxLength4G(self, size): + # gh-162153: a decompress() call whose input exceeds UINT_MAX + # bytes must not silently drop the unconsumed remainder when + # max_length is reached in the same call. + # Peak memory is about 2x size: the >4 GiB compressed input and + # the decompressor's internal copy of the unconsumed input tail + # coexist across the first decompress() call. + blocksize = min(10 * 1024 * 1024, size) + block = random.randbytes(blocksize) + nblocks = (size - 1) // blocksize + 1 + # Level 0 (stored blocks) keeps the compressed input above + # 4 GiB, so arrange_input_buffer() has to chunk it. + c = zlib.compressobj(0) + compressed = bytearray() + try: + for _ in range(nblocks): + compressed += c.compress(block) + compressed += c.flush() + zlibd = zlib._ZlibDecompressor() + out = len(zlibd.decompress(compressed, 65536)) + finally: + block = compressed = None + while not zlibd.eof and not zlibd.needs_input: + out += len(zlibd.decompress(b"", 65536)) + self.assertTrue(zlibd.eof) + self.assertEqual(out, blocksize * nblocks) + def testPickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.assertRaises(TypeError): From a7463baabc58b45739c655417a1c7fdbb6a77de9 Mon Sep 17 00:00:00 2001 From: Aathithya Arasu S <142731071+Aathi-27@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:17:21 +0530 Subject: [PATCH 3/4] gh-162153: Add NEWS entry Fixes an issue where input exceeding 4 GiB was not handled correctly, leading to incorrect stream position during decompression. --- .../next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst b/Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst new file mode 100644 index 000000000000000..b05b2b56635fe05 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst @@ -0,0 +1,4 @@ +Fix silent input loss in :meth:`!zlib._ZlibDecompressor.decompress` when one +call receives more than 4 GiB of input and reaches *max_length*: the +unconsumed remainder of an internal input chunk was dropped from the +bookkeeping, so decompression continued at the wrong stream position. From 94c7048941d6e88fdc9f663d192d5d82368b71cf Mon Sep 17 00:00:00 2001 From: Aathithya Arasu S <142731071+Aathi-27@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:19:50 +0530 Subject: [PATCH 4/4] Rename 2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst to 2026-11-18-22-01-00.gh-issue-162153.Zl1BmX.rst --- ...bmaxlen.rst => 2026-11-18-22-01-00.gh-issue-162153.Zl1BmX.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst => 2026-11-18-22-01-00.gh-issue-162153.Zl1BmX.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst b/Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-issue-162153.Zl1BmX.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-162153.zlibmaxlen.rst rename to Misc/NEWS.d/next/Library/2026-11-18-22-01-00.gh-issue-162153.Zl1BmX.rst