Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa0e7af

Browse files
committed
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.
2 parents 38d65df + 8dd49fe commit aa0e7af

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/test/test_tracemalloc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ def test_sys_xoptions_invalid(self):
807807
b'number of frames',
808808
stderr)
809809

810+
def test_pymem_alloc0(self):
811+
# Issue #21639: Check that PyMem_Malloc(0) with tracemalloc enabled
812+
# does not crash.
813+
code = 'import _testcapi; _testcapi.test_pymem_alloc0(); 1'
814+
assert_python_ok('-X', 'tracemalloc', '-c', code)
815+
810816

811817
def test_main():
812818
support.run_unittest(

Modules/_tracemalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
478478
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
479479
void *ptr;
480480

481-
assert(nelem <= PY_SIZE_MAX / elsize);
481+
assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize);
482482

483483
if (use_calloc)
484484
ptr = alloc->calloc(alloc->ctx, nelem, elsize);

0 commit comments

Comments
 (0)