Skip to content

Commit 5064a52

Browse files
committed
Issue python#18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
1 parent 65bf9cf commit 5064a52

3 files changed

Lines changed: 77 additions & 8 deletions

File tree

‎Modules/_bz2module.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ BZ2Compressor_flush(BZ2Compressor *self, PyObject *noargs)
248248
return result;
249249
}
250250

251+
static void*
252+
BZ2_Malloc(void* ctx, int items, int size)
253+
{
254+
if (items < 0 || size < 0)
255+
return NULL;
256+
if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
257+
return NULL;
258+
/* PyMem_Malloc() cannot be used: compress() and decompress()
259+
release the GIL */
260+
return PyMem_RawMalloc(items * size);
261+
}
262+
263+
static void
264+
BZ2_Free(void* ctx, void *ptr)
265+
{
266+
return PyMem_RawFree(ptr);
267+
}
268+
251269
static int
252270
BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs)
253271
{
@@ -270,6 +288,9 @@ BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs)
270288
}
271289
#endif
272290

291+
self->bzs.opaque = NULL;
292+
self->bzs.bzalloc = BZ2_Malloc;
293+
self->bzs.bzfree = BZ2_Free;
273294
bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
274295
if (catch_bz2_error(bzerror))
275296
goto error;

‎Modules/_lzmamodule.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum {
5151

5252
typedef struct {
5353
PyObject_HEAD
54+
lzma_allocator alloc;
5455
lzma_stream lzs;
5556
int flushed;
5657
#ifdef WITH_THREAD
@@ -60,6 +61,7 @@ typedef struct {
6061

6162
typedef struct {
6263
PyObject_HEAD
64+
lzma_allocator alloc;
6365
lzma_stream lzs;
6466
int check;
6567
char eof;
@@ -117,6 +119,22 @@ catch_lzma_error(lzma_ret lzret)
117119
}
118120
}
119121

122+
static void*
123+
PyLzma_Malloc(void *opaque, size_t items, size_t size)
124+
{
125+
if (items > (size_t)PY_SSIZE_T_MAX / size)
126+
return NULL;
127+
/* PyMem_Malloc() cannot be used:
128+
the GIL is not held when lzma_code() is called */
129+
return PyMem_RawMalloc(items * size);
130+
}
131+
132+
static void
133+
PyLzma_Free(void *opaque, void *ptr)
134+
{
135+
return PyMem_RawFree(ptr);
136+
}
137+
120138
#if BUFSIZ < 8192
121139
#define INITIAL_BUFFER_SIZE 8192
122140
#else
@@ -656,6 +674,11 @@ Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs)
656674
if (!uint32_converter(preset_obj, &preset))
657675
return -1;
658676

677+
self->alloc.opaque = NULL;
678+
self->alloc.alloc = PyLzma_Malloc;
679+
self->alloc.free = PyLzma_Free;
680+
self->lzs.allocator = &self->alloc;
681+
659682
#ifdef WITH_THREAD
660683
self->lock = PyThread_allocate_lock();
661684
if (self->lock == NULL) {
@@ -922,6 +945,11 @@ Decompressor_init(Decompressor *self, PyObject *args, PyObject *kwargs)
922945
return -1;
923946
}
924947

948+
self->alloc.opaque = NULL;
949+
self->alloc.alloc = PyLzma_Malloc;
950+
self->alloc.free = PyLzma_Free;
951+
self->lzs.allocator = &self->alloc;
952+
925953
#ifdef WITH_THREAD
926954
self->lock = PyThread_allocate_lock();
927955
if (self->lock == NULL) {

‎Modules/zlibmodule.c‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ newcompobject(PyTypeObject *type)
136136
return self;
137137
}
138138

139+
static void*
140+
PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
141+
{
142+
if (items > (size_t)PY_SSIZE_T_MAX / size)
143+
return NULL;
144+
/* PyMem_Malloc() cannot be used: the GIL is not held when
145+
inflate() and deflate() are called */
146+
return PyMem_RawMalloc(items * size);
147+
}
148+
149+
static void
150+
PyZlib_Free(voidpf ctx, void *ptr)
151+
{
152+
return PyMem_RawFree(ptr);
153+
}
154+
139155
PyDoc_STRVAR(compress__doc__,
140156
"compress(string[, level]) -- Returned compressed string.\n"
141157
"\n"
@@ -175,8 +191,9 @@ PyZlib_compress(PyObject *self, PyObject *args)
175191
/* Past the point of no return. From here on out, we need to make sure
176192
we clean up mallocs & INCREFs. */
177193

178-
zst.zalloc = (alloc_func)NULL;
179-
zst.zfree = (free_func)Z_NULL;
194+
zst.opaque = NULL;
195+
zst.zalloc = PyZlib_Malloc;
196+
zst.zfree = PyZlib_Free;
180197
zst.next_out = (Byte *)output;
181198
zst.next_in = (Byte *)input;
182199
zst.avail_in = length;
@@ -262,8 +279,9 @@ PyZlib_decompress(PyObject *self, PyObject *args)
262279
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
263280
goto error;
264281

265-
zst.zalloc = (alloc_func)NULL;
266-
zst.zfree = (free_func)Z_NULL;
282+
zst.opaque = NULL;
283+
zst.zalloc = PyZlib_Malloc;
284+
zst.zfree = PyZlib_Free;
267285
zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
268286
zst.next_in = (Byte *)input;
269287
err = inflateInit2(&zst, wsize);
@@ -356,8 +374,9 @@ PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
356374
self = newcompobject(&Comptype);
357375
if (self==NULL)
358376
goto error;
359-
self->zst.zalloc = (alloc_func)NULL;
360-
self->zst.zfree = (free_func)Z_NULL;
377+
self->zst.opaque = NULL;
378+
self->zst.zalloc = PyZlib_Malloc;
379+
self->zst.zfree = PyZlib_Free;
361380
self->zst.next_in = NULL;
362381
self->zst.avail_in = 0;
363382
err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
@@ -420,8 +439,9 @@ PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
420439
self = newcompobject(&Decomptype);
421440
if (self == NULL)
422441
return(NULL);
423-
self->zst.zalloc = (alloc_func)NULL;
424-
self->zst.zfree = (free_func)Z_NULL;
442+
self->zst.opaque = NULL;
443+
self->zst.zalloc = PyZlib_Malloc;
444+
self->zst.zfree = PyZlib_Free;
425445
self->zst.next_in = NULL;
426446
self->zst.avail_in = 0;
427447
if (zdict != NULL) {

0 commit comments

Comments
 (0)