[Python-checkins] cpython (merge 3.4 -> default): Issue #21639: Fix a division by zero in tracemalloc on calloc(0, 0). The

victor.stinner python-checkins at python.org
Mon Jun 2 21:41:05 CEST 2014


http://hg.python.org/cpython/rev/1505124c0df4
changeset:   90979:1505124c0df4
parent:      90977:7083634065c9
parent:      90978:6f362a702242
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jun 02 21:40:22 2014 +0200
summary:
  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.

files:
  Lib/test/test_tracemalloc.py |  6 ++++++
  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
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -807,6 +807,12 @@
                                   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
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -478,7 +478,7 @@
     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);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list