From 7e138027ff22ab9a75a8af893caf3f698287df38 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Fri, 26 Jul 2013 15:03:50 +0200 Subject: [PATCH 1/2] Fix memory leaks and add checks for failing malloc() calls to testcapi module CID 1058288 --- Modules/_testcapimodule.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 7a007c749d6..6f87c130c54 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2183,6 +2183,8 @@ profile_int(PyObject *self, PyObject* args) /* 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++) { @@ -2194,10 +2196,13 @@ profile_int(PyObject *self, PyObject* args) } 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++) { @@ -2209,9 +2214,12 @@ profile_int(PyObject *self, PyObject* args) } 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++) { @@ -2223,6 +2231,7 @@ profile_int(PyObject *self, PyObject* args) } gettimeofday(&stop, NULL); print_delta(5, &start, &stop); + free(multiple); /* Test 6: Perform small int addition */ op1 = PyLong_FromLong(1); From 3205e74d880a7cacd52ff032325de28acdec888d Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Fri, 26 Jul 2013 15:06:48 +0200 Subject: [PATCH 2/2] Fix declaration-after-statement of d49f65ff4f3c --- Modules/_testcapimodule.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 6f87c130c54..4fd2d9fe1f2 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -1645,11 +1645,15 @@ test_long_numbits(PyObject *self) 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)