mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Fix thread locks in zlib module may go wrong in rare case. (#22126)
Setting `next_in` before acquiring the thread lock may mix up compress/decompress state in other threads.
This commit is contained in:
		
							parent
							
								
									878bc8b6c2
								
							
						
					
					
						commit
						93f411838a
					
				
					 2 changed files with 11 additions and 9 deletions
				
			
		| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					Fix thread locks in zlib module may go wrong in rare case. Patch by Ma Lin.
 | 
				
			||||||
| 
						 | 
					@ -10,10 +10,12 @@
 | 
				
			||||||
#include "zlib.h"
 | 
					#include "zlib.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define ENTER_ZLIB(obj) \
 | 
					#define ENTER_ZLIB(obj) do {                      \
 | 
				
			||||||
    Py_BEGIN_ALLOW_THREADS; \
 | 
					    if (!PyThread_acquire_lock((obj)->lock, 0)) { \
 | 
				
			||||||
 | 
					        Py_BEGIN_ALLOW_THREADS                    \
 | 
				
			||||||
        PyThread_acquire_lock((obj)->lock, 1);    \
 | 
					        PyThread_acquire_lock((obj)->lock, 1);    \
 | 
				
			||||||
    Py_END_ALLOW_THREADS;
 | 
					        Py_END_ALLOW_THREADS                      \
 | 
				
			||||||
 | 
					    } } while (0)
 | 
				
			||||||
#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
 | 
					#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
 | 
					#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
 | 
				
			||||||
| 
						 | 
					@ -634,14 +636,13 @@ zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
 | 
				
			||||||
    PyObject *RetVal = NULL;
 | 
					    PyObject *RetVal = NULL;
 | 
				
			||||||
    Py_ssize_t obuflen = DEF_BUF_SIZE;
 | 
					    Py_ssize_t obuflen = DEF_BUF_SIZE;
 | 
				
			||||||
    int err;
 | 
					    int err;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    zlibstate *state = PyType_GetModuleState(cls);
 | 
					    zlibstate *state = PyType_GetModuleState(cls);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ENTER_ZLIB(self);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    self->zst.next_in = data->buf;
 | 
					    self->zst.next_in = data->buf;
 | 
				
			||||||
    Py_ssize_t ibuflen = data->len;
 | 
					    Py_ssize_t ibuflen = data->len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ENTER_ZLIB(self);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
        arrange_input_buffer(&self->zst, &ibuflen);
 | 
					        arrange_input_buffer(&self->zst, &ibuflen);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -761,6 +762,8 @@ zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
 | 
				
			||||||
    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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -768,8 +771,6 @@ zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
 | 
				
			||||||
    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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue