]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35090: Fix potential division by zero in allocator wrappers (GH-10174)
authorAlexey Izbyshev <izbyshev@ispras.ru>
Sun, 28 Oct 2018 16:45:50 +0000 (19:45 +0300)
committerVictor Stinner <vstinner@redhat.com>
Sun, 28 Oct 2018 16:45:50 +0000 (17:45 +0100)
* Fix potential division by zero in BZ2_Malloc()
* Avoid division by zero in PyLzma_Malloc()
* Avoid division by zero and integer overflow in PyZlib_Malloc()

Reported by Svace static analyzer.

Modules/_bz2module.c
Modules/_lzmamodule.c
Modules/zlibmodule.c

index 3890b60b1b87b3145ca8a27136242d7389d65c25..f0d9588fe55d6f9f8e50cf54cb3885359106f886 100644 (file)
@@ -277,11 +277,11 @@ BZ2_Malloc(void* ctx, int items, int size)
 {
     if (items < 0 || size < 0)
         return NULL;
-    if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
+    if (size != 0 && (size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
         return NULL;
     /* PyMem_Malloc() cannot be used: compress() and decompress()
        release the GIL */
-    return PyMem_RawMalloc(items * size);
+    return PyMem_RawMalloc((size_t)items * (size_t)size);
 }
 
 static void
index 7b501d8202d8b99e927f42708ae86349dc279b0d..bb7a7ec50ce059bfff621ea273478ba5a0704634 100644 (file)
@@ -108,7 +108,7 @@ 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)
+    if (size != 0 && 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 */
index 36a3835e421fbb7a30e06e0aecfe7bb4a3038177..00bbe21fc0bd827668d9f8cb41de9cd2bd1ae52d 100644 (file)
@@ -117,11 +117,11 @@ newcompobject(PyTypeObject *type)
 static void*
 PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
 {
-    if (items > (size_t)PY_SSIZE_T_MAX / size)
+    if (size != 0 && 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);
+    return PyMem_RawMalloc((size_t)items * (size_t)size);
 }
 
 static void