https://github.com/python/cpython/commit/e16b12bb982bc916664850da1d0a78109b2145b8
commit: e16b12bb982bc916664850da1d0a78109b2145b8
branch: 3.14
author: Stan Ulbrych <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-09-26T17:18:25+01:00
summary:

[3.14] gh-158169: Clear `zlib.Decompress.unconsumed_tail` at end of stream 
(GH-158177) (#158229)

(cherry picked from commit 8122ff4818f418e8ae984cf6964215689b00808d)

Co-authored-by: GruffElixir <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
M Lib/test/test_zlib.py
M Modules/zlibmodule.c

diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index ed2eb0e05eb532..9ef974b3e37d0f 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -651,6 +651,18 @@ def test_decompress_unused_data(self):
                 self.assertEqual(dco.unconsumed_tail, b'')
                 self.assertEqual(dco.unused_data, remainder)
 
+    def test_decompress_unconsumed_tail_after_eof(self):
+        source = b'abcdefghijklmnopqrstuvwxyz'
+        remainder = b'0123456789'
+        dco = zlib.decompressobj()
+        data = dco.decompress(zlib.compress(source) + remainder, 1)
+        data += dco.decompress(dco.unconsumed_tail)
+
+        self.assertTrue(dco.eof)
+        self.assertEqual(data, source)
+        self.assertEqual(dco.unconsumed_tail, b'')
+        self.assertEqual(dco.unused_data, remainder)
+
     # issue27164
     def test_decompress_raw_with_dictionary(self):
         zdict = b'abcdefghijklmnopqrstuvwxyz'
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst 
b/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
new file mode 100644
index 00000000000000..7c965451c056a4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-25-22-15-54.gh-issue-158169.ZlibTail.rst
@@ -0,0 +1,2 @@
+Fix :attr:`zlib.Decompress.unconsumed_tail` duplicating
+:attr:`~zlib.Decompress.unused_data` after end of stream when *max_length* was 
used.
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 095e3e2b7dfacd..fdd56534bca7ed 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -842,6 +842,7 @@ save_unconsumed_input(compobject *self, Py_buffer *data, 
int err)
             memcpy(PyBytes_AS_STRING(new_data) + old_size,
                       self->zst.next_in, left_size);
             Py_SETREF(self->unused_data, new_data);
+            self->zst.next_in += left_size;
             self->zst.avail_in = 0;
         }
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to