Skip to content

Commit 196b8f3

Browse files
author
Ma Lin
authored
Fix thread locks in zlib module may go wrong in rare case (python#22132)
Setting `next_in` before acquiring the thread lock may mix up compress/decompress state in other threads.
1 parent b570fea commit 196b8f3

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix thread locks in zlib module may go wrong in rare case. Patch by Ma Lin.

Modules/zlibmodule.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,11 @@ zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
653653
Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
654654
int err;
655655

656+
ENTER_ZLIB(self);
657+
656658
self->zst.next_in = data->buf;
657659
ibuflen = data->len;
658660

659-
ENTER_ZLIB(self);
660-
661661
do {
662662
arrange_input_buffer(&self->zst, &ibuflen);
663663

@@ -771,15 +771,15 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
771771
else
772772
hard_limit = max_length;
773773

774+
ENTER_ZLIB(self);
775+
774776
self->zst.next_in = data->buf;
775777
ibuflen = data->len;
776778

777779
/* limit amount of data allocated to max_length */
778780
if (max_length && obuflen > max_length)
779781
obuflen = max_length;
780782

781-
ENTER_ZLIB(self);
782-
783783
do {
784784
arrange_input_buffer(&self->zst, &ibuflen);
785785

0 commit comments

Comments
 (0)