From: Victor Stinner Date: Mon, 2 Jun 2014 19:40:22 +0000 (+0200) Subject: Issue #21639: Fix a division by zero in tracemalloc on calloc(0, 0). The X-Git-Tag: v3.5.0a1~1537 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aa0e7afa438d7586353a3338fd350449714e117a;p=python Issue #21639: Fix a division by zero in tracemalloc on calloc(0, 0). The regression was introduced recently with the introduction of the new "calloc" functions (PyMem_RawCalloc, PyMem_Calloc, PyObject_Calloc). Add also a unit test to check for the non-regression. --- aa0e7afa438d7586353a3338fd350449714e117a diff --cc Modules/_tracemalloc.c index 429b209c02,780e8eda82..1e454144de --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@@ -478,12 -481,7 +478,12 @@@ tracemalloc_alloc(int use_calloc, void PyMemAllocator *alloc = (PyMemAllocator *)ctx; void *ptr; - assert(nelem <= PY_SIZE_MAX / elsize); - ptr = alloc->malloc(alloc->ctx, size); ++ assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize); + + if (use_calloc) + ptr = alloc->calloc(alloc->ctx, nelem, elsize); + else + ptr = alloc->malloc(alloc->ctx, nelem * elsize); if (ptr == NULL) return NULL;