diff options
-rw-r--r-- | Lib/test/test_tracemalloc.py | 6 | ||||
-rw-r--r-- | Modules/_tracemalloc.c | 2 |
2 files changed, 7 insertions, 1 deletions
diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index c953885..33b0dc2 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -807,6 +807,12 @@ class TestCommandLine(unittest.TestCase): b'number of frames', stderr) + def test_pymem_alloc0(self): + # Issue #21639: Check that PyMem_Malloc(0) with tracemalloc enabled + # does not crash. + code = 'import _testcapi; _testcapi.test_pymem_alloc0(); 1' + assert_python_ok('-X', 'tracemalloc', '-c', code) + def test_main(): support.run_unittest( diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 429b209..1e45414 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -478,7 +478,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) PyMemAllocator *alloc = (PyMemAllocator *)ctx; void *ptr; - assert(nelem <= PY_SIZE_MAX / elsize); + assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize); if (use_calloc) ptr = alloc->calloc(alloc->ctx, nelem, elsize); |