[3.10] bpo-41735: Fix thread lock in zlib.Decompress.flush() may go wrong (GH-29587) (GH-29811)



* Fix thread lock in zlib.Decompress.flush() may go wrong

Getting `.unconsumed_tail` before acquiring the thread lock may mix up decompress state.
(cherry picked from commit 7edb6270a78c695e4c2ae2432797dc18105374fc)


Co-authored-by: Ma Lin <animalize@users.noreply.github.com>

Automerge-Triggered-By: GH:gpshead
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 68de701..a6940f2 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -1266,12 +1266,13 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
         return NULL;
     }
 
+    ENTER_ZLIB(self);
+
     if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
+        LEAVE_ZLIB(self);
         return NULL;
     }
 
-    ENTER_ZLIB(self);
-
     self->zst.next_in = data.buf;
     ibuflen = data.len;