[Python-checkins] cpython (merge 3.3 -> default): Fix memory leaks and add checks for failing malloc() calls to testcapi module

christian.heimes python-checkins at python.org
Fri Jul 26 15:07:50 CEST 2013


http://hg.python.org/cpython/rev/f82fe2b43165
changeset:   84835:f82fe2b43165
parent:      84832:33891989c9cf
parent:      84834:708811f8470f
user:        Christian Heimes <christian at cheimes.de>
date:        Fri Jul 26 15:07:34 2013 +0200
summary:
  Fix memory leaks and add checks for failing malloc() calls to testcapi module
CID 1058288
Fix declaration-after-statement of d49f65ff4f3c

files:
  Modules/_testcapimodule.c |  19 ++++++++++++++++---
  1 files changed, 16 insertions(+), 3 deletions(-)


diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1659,11 +1659,15 @@
     int i;
 
     for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
-        PyObject *plong = PyLong_FromLong(testcases[i].input);
+        size_t nbits;
+        int sign;
+        PyObject *plong;
+
+        plong = PyLong_FromLong(testcases[i].input);
         if (plong == NULL)
             return NULL;
-        size_t nbits = _PyLong_NumBits(plong);
-        int sign = _PyLong_Sign(plong);
+        nbits = _PyLong_NumBits(plong);
+        sign = _PyLong_Sign(plong);
 
         Py_DECREF(plong);
         if (nbits != testcases[i].nbits)
@@ -2197,6 +2201,8 @@
     /* Test 3: Allocate a few integers, then release
        them all simultaneously. */
     multiple = malloc(sizeof(PyObject*) * 1000);
+    if (multiple == NULL)
+        return PyErr_NoMemory();
     gettimeofday(&start, NULL);
     for(k=0; k < 20000; k++) {
         for(i=0; i < 1000; i++) {
@@ -2208,10 +2214,13 @@
     }
     gettimeofday(&stop, NULL);
     print_delta(3, &start, &stop);
+    free(multiple);
 
     /* Test 4: Allocate many integers, then release
        them all simultaneously. */
     multiple = malloc(sizeof(PyObject*) * 1000000);
+    if (multiple == NULL)
+        return PyErr_NoMemory();
     gettimeofday(&start, NULL);
     for(k=0; k < 20; k++) {
         for(i=0; i < 1000000; i++) {
@@ -2223,9 +2232,12 @@
     }
     gettimeofday(&stop, NULL);
     print_delta(4, &start, &stop);
+    free(multiple);
 
     /* Test 5: Allocate many integers < 32000 */
     multiple = malloc(sizeof(PyObject*) * 1000000);
+    if (multiple == NULL)
+        return PyErr_NoMemory();
     gettimeofday(&start, NULL);
     for(k=0; k < 10; k++) {
         for(i=0; i < 1000000; i++) {
@@ -2237,6 +2249,7 @@
     }
     gettimeofday(&stop, NULL);
     print_delta(5, &start, &stop);
+    free(multiple);
 
     /* Test 6: Perform small int addition */
     op1 = PyLong_FromLong(1);

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


More information about the Python-checkins mailing list