8000 bpo-41735: Fix thread locks in zlib module may go wrong in rare case. · Pull Request #22126 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41735: Fix thread locks in zlib module may go wrong in rare case. #22126

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix thread locks in zlib module may go wrong in rare case. Patch by Ma Lin.
19 changes: 10 additions & 9 deletions Modules/zlibmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#include "zlib.h"


#define ENTER_ZLIB(obj) \
Py_BEGIN_ALLOW_THREADS; \
PyThread_acquire_lock((obj)->lock, 1); \
Py_END_ALLOW_THREADS;
#define ENTER_ZLIB(obj) do { \
if (!PyThread_acquire_lock((obj)->lock, 0)) { \
Py_BEGIN_ALLOW_THREADS \
PyThread_acquire_lock((obj)->lock, 1); \
Py_END_ALLOW_THREADS \
} } while (0)
#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);

#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Expand Down Expand Up @@ -634,14 +636,13 @@ zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
PyObject *RetVal = NULL;
Py_ssize_t obuflen = DEF_BUF_SIZE;
int err;

zlibstate *state = PyType_GetModuleState(cls);

ENTER_ZLIB(self);

self->zst.next_in = data->buf;
Py_ssize_t ibuflen = data->len;

ENTER_ZLIB(self);

do {
arrange_input_buffer(&self->zst, &ibuflen);

Expand Down Expand Up @@ -761,15 +762,15 @@ zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
else
hard_limit = max_length;

ENTER_ZLIB(self);

self->zst.next_in = data->buf;
ibuflen = data->len;

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

ENTER_ZLIB(self);

do {
arrange_input_buffer(&self->zst, &ibuflen);

Expand Down
0