From b3a3a811b2a18d90f0925915d9d44659b5f6b218 Mon Sep 17 00:00:00 2001 From: animalize Date: Mon, 7 Sep 2020 21:39:06 +0800 Subject: [PATCH] Fix thread locks in zlib module may go wrong in rare case Setting `next_in` before acquiring the thread lock may mix up compress/decompress state in other threads. --- .../next/Library/2020-09-07-21-40-07.bpo-41735.NKqGKy.rst | 1 + Modules/zlibmodule.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-09-07-21-40-07.bpo-41735.NKqGKy.rst diff --git a/Misc/NEWS.d/next/Library/2020-09-07-21-40-07.bpo-41735.NKqGKy.rst b/Misc/NEWS.d/next/Library/2020-09-07-21-40-07.bpo-41735.NKqGKy.rst new file mode 100644 index 00000000000..9e36435a364 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-07-21-40-07.bpo-41735.NKqGKy.rst @@ -0,0 +1 @@ +Fix thread locks in zlib module may go wrong in rare case. Patch by Ma Lin. diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index fe27909ae8a..6c14c3adbf2 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -667,11 +667,11 @@ zlib_Compress_compress_impl(compobject *self, Py_buffer *data) Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE; int err; + ENTER_ZLIB(self); + self->zst.next_in = data->buf; ibuflen = data->len; - ENTER_ZLIB(self); - do { arrange_input_buffer(&self->zst, &ibuflen); @@ -785,6 +785,8 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, else hard_limit = max_length; + ENTER_ZLIB(self); + self->zst.next_in = data->buf; ibuflen = data->len; @@ -792,8 +794,6 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, if (max_length && obuflen > max_length) obuflen = max_length; - ENTER_ZLIB(self); - do { arrange_input_buffer(&self->zst, &ibuflen);