diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-07-07 14:50:27 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-07-07 14:50:27 (GMT) |
commit | 5064a52bcb59fd3ea56e5f57502946eb87eeaf14 (patch) | |
tree | 0ffd0330bcd9f5ec34416ec48ad7d43985d597dc /Modules/_lzmamodule.c | |
parent | 65bf9cf26fee75dbe7e00f01e236bc5107a09a40 (diff) | |
download | cpython-5064a52bcb59fd3ea56e5f57502946eb87eeaf14.zip cpython-5064a52bcb59fd3ea56e5f57502946eb87eeaf14.tar.gz cpython-5064a52bcb59fd3ea56e5f57502946eb87eeaf14.tar.bz2 |
Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Diffstat (limited to 'Modules/_lzmamodule.c')
-rw-r--r-- | Modules/_lzmamodule.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index b482a77..711604d 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -51,6 +51,7 @@ enum { typedef struct { PyObject_HEAD + lzma_allocator alloc; lzma_stream lzs; int flushed; #ifdef WITH_THREAD @@ -60,6 +61,7 @@ typedef struct { typedef struct { PyObject_HEAD + lzma_allocator alloc; lzma_stream lzs; int check; char eof; @@ -117,6 +119,22 @@ catch_lzma_error(lzma_ret lzret) } } +static void* +PyLzma_Malloc(void *opaque, size_t items, size_t size) +{ + if (items > (size_t)PY_SSIZE_T_MAX / size) + return NULL; + /* PyMem_Malloc() cannot be used: + the GIL is not held when lzma_code() is called */ + return PyMem_RawMalloc(items * size); +} + +static void +PyLzma_Free(void *opaque, void *ptr) +{ + return PyMem_RawFree(ptr); +} + #if BUFSIZ < 8192 #define INITIAL_BUFFER_SIZE 8192 #else @@ -656,6 +674,11 @@ Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs) if (!uint32_converter(preset_obj, &preset)) return -1; + self->alloc.opaque = NULL; + self->alloc.alloc = PyLzma_Malloc; + self->alloc.free = PyLzma_Free; + self->lzs.allocator = &self->alloc; + #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (self->lock == NULL) { @@ -922,6 +945,11 @@ Decompressor_init(Decompressor *self, PyObject *args, PyObject *kwargs) return -1; } + self->alloc.opaque = NULL; + self->alloc.alloc = PyLzma_Malloc; + self->alloc.free = PyLzma_Free; + self->lzs.allocator = &self->alloc; + #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (self->lock == NULL) { |