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/zlibmodule.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/zlibmodule.c')
-rw-r--r-- | Modules/zlibmodule.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 30147ae..2e5f473 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -136,6 +136,22 @@ newcompobject(PyTypeObject *type) return self; } +static void* +PyZlib_Malloc(voidpf ctx, uInt items, uInt size) +{ + if (items > (size_t)PY_SSIZE_T_MAX / size) + return NULL; + /* PyMem_Malloc() cannot be used: the GIL is not held when + inflate() and deflate() are called */ + return PyMem_RawMalloc(items * size); +} + +static void +PyZlib_Free(voidpf ctx, void *ptr) +{ + return PyMem_RawFree(ptr); +} + PyDoc_STRVAR(compress__doc__, "compress(string[, level]) -- Returned compressed string.\n" "\n" @@ -175,8 +191,9 @@ PyZlib_compress(PyObject *self, PyObject *args) /* Past the point of no return. From here on out, we need to make sure we clean up mallocs & INCREFs. */ - zst.zalloc = (alloc_func)NULL; - zst.zfree = (free_func)Z_NULL; + zst.opaque = NULL; + zst.zalloc = PyZlib_Malloc; + zst.zfree = PyZlib_Free; zst.next_out = (Byte *)output; zst.next_in = (Byte *)input; zst.avail_in = length; @@ -262,8 +279,9 @@ PyZlib_decompress(PyObject *self, PyObject *args) if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) goto error; - zst.zalloc = (alloc_func)NULL; - zst.zfree = (free_func)Z_NULL; + zst.opaque = NULL; + zst.zalloc = PyZlib_Malloc; + zst.zfree = PyZlib_Free; zst.next_out = (Byte *)PyBytes_AS_STRING(result_str); zst.next_in = (Byte *)input; err = inflateInit2(&zst, wsize); @@ -356,8 +374,9 @@ PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs) self = newcompobject(&Comptype); if (self==NULL) goto error; - self->zst.zalloc = (alloc_func)NULL; - self->zst.zfree = (free_func)Z_NULL; + self->zst.opaque = NULL; + self->zst.zalloc = PyZlib_Malloc; + self->zst.zfree = PyZlib_Free; self->zst.next_in = NULL; self->zst.avail_in = 0; err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); @@ -420,8 +439,9 @@ PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs) self = newcompobject(&Decomptype); if (self == NULL) return(NULL); - self->zst.zalloc = (alloc_func)NULL; - self->zst.zfree = (free_func)Z_NULL; + self->zst.opaque = NULL; + self->zst.zalloc = PyZlib_Malloc; + self->zst.zfree = PyZlib_Free; self->zst.next_in = NULL; self->zst.avail_in = 0; if (zdict != NULL) { |