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.
This commit is contained in:
animalize 2020-09-07 21:39:06 +08:00
parent b6d2acb1b1
commit b3a3a811b2
2 changed files with 5 additions and 4 deletions

View File

@ -0,0 +1 @@
Fix thread locks in zlib module may go wrong in rare case. Patch by Ma Lin.

View File

@ -667,11 +667,11 @@ zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE; Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
int err; int err;
ENTER_ZLIB(self);
self->zst.next_in = data->buf; self->zst.next_in = data->buf;
ibuflen = data->len; ibuflen = data->len;
ENTER_ZLIB(self);
do { do {
arrange_input_buffer(&self->zst, &ibuflen); arrange_input_buffer(&self->zst, &ibuflen);
@ -785,6 +785,8 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
else else
hard_limit = max_length; hard_limit = max_length;
ENTER_ZLIB(self);
self->zst.next_in = data->buf; self->zst.next_in = data->buf;
ibuflen = data->len; ibuflen = data->len;
@ -792,8 +794,6 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
if (max_length && obuflen > max_length) if (max_length && obuflen > max_length)
obuflen = max_length; obuflen = max_length;
ENTER_ZLIB(self);
do { do {
arrange_input_buffer(&self->zst, &ibuflen); arrange_input_buffer(&self->zst, &ibuflen);