From 86c1265cdc64030c8921e0da5fcae2ac64299c26 Mon Sep 17 00:00:00 2001 From: Ma Lin Date: Sat, 27 Nov 2021 08:21:22 +0800 Subject: [PATCH] [3.9] bpo-41735: Fix thread lock in zlib.Decompress.flush() may go wrong (GH-29588) * Fix thread lock in zlib.Decompress.flush() may go wrong Getting `.unconsumed_tail` before acquiring the thread lock may mix up decompress state. backport of https://github.com/python/cpython/pull/29587 to 3.9/3.8. --- .../next/Library/2021-11-17-11-38-30.bpo-41735.2feh9v.rst | 1 + Modules/zlibmodule.c | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-11-17-11-38-30.bpo-41735.2feh9v.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-17-11-38-30.bpo-41735.2feh9v.rst b/Misc/NEWS.d/next/Library/2021-11-17-11-38-30.bpo-41735.2feh9v.rst new file mode 100644 index 000000000000..101da0e9ce64 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-11-17-11-38-30.bpo-41735.2feh9v.rst @@ -0,0 +1 @@ +Fix thread lock in ``zlib.Decompress.flush()`` method before ``PyObject_GetBuffer``. diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 8f8bcd85f2c1..4dfd4ae67220 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -1134,11 +1134,13 @@ zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length) return NULL; } - if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) - return NULL; - ENTER_ZLIB(self); + if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) { + LEAVE_ZLIB(self); + return NULL; + } + self->zst.next_in = data.buf; ibuflen = data.len; -- 2.47.3