Skip to content

Commit 3d4fabb

Browse files
Alexey Izbyshevvstinner
authored andcommitted
bpo-35090: Fix potential division by zero in allocator wrappers (pythonGH-10174)
* 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.
1 parent 68d6dc0 commit 3d4fabb

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

Modules/_bz2module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ BZ2_Malloc(void* ctx, int items, int size)
277277
{
278278
if (items < 0 || size < 0)
279279
return NULL;
280-
if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
280+
if (size != 0 && (size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
281281
return NULL;
282282
/* PyMem_Malloc() cannot be used: compress() and decompress()
283283
release the GIL */
284-
return PyMem_RawMalloc(items * size);
284+
return PyMem_RawMalloc((size_t)items * (size_t)size);
285285
}
286286

287287
static void

Modules/_lzmamodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ catch_lzma_error(lzma_ret lzret)
108108
static void*
109109
PyLzma_Malloc(void *opaque, size_t items, size_t size)
110110
{
111-
if (items > (size_t)PY_SSIZE_T_MAX / size)
111+
if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
112112
return NULL;
113113
/* PyMem_Malloc() cannot be used:
114114
the GIL is not held when lzma_code() is called */

Modules/zlibmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ newcompobject(PyTypeObject *type)
117117
static void*
118118
PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
119119
{
120-
if (items > (size_t)PY_SSIZE_T_MAX / size)
120+
if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
121121
return NULL;
122122
/* PyMem_Malloc() cannot be used: the GIL is not held when
123123
inflate() and deflate() are called */
124-
return PyMem_RawMalloc(items * size);
124+
return PyMem_RawMalloc((size_t)items * (size_t)size);
125125
}
126126

127127
static void

0 commit comments

Comments
 (0)