]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #14909: A number of places were using PyMem_Realloc() apis and
authorKristjan Valur Jonsson <sweskman@gmail.com>
Thu, 31 May 2012 09:37:31 +0000 (09:37 +0000)
committerKristjan Valur Jonsson <sweskman@gmail.com>
Thu, 31 May 2012 09:37:31 +0000 (09:37 +0000)
PyObject_GC_Resize() with incorrect error handling.  In case of errors,
the original object would be leaked.  This checkin fixes those cases.

Modules/_localemodule.c
Modules/_randommodule.c
Modules/unicodedata.c
Objects/frameobject.c
Objects/unicodeobject.c

index 20c59a4e1ad09230dfbdb29a1ceeffe2fcf315e2..cc688ba1406a9f7195866bc2bbaca11204ad0736 100644 (file)
@@ -257,11 +257,12 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
     n2 = wcsxfrm(buf, s, n1);
     if (n2 >= (size_t)n1) {
         /* more space needed */
-        buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
-        if (!buf) {
+        wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
+        if (!new_buf) {
             PyErr_NoMemory();
             goto exit;
         }
+        buf = new_buf;
         n2 = wcsxfrm(buf, s, n2+1);
     }
     result = PyUnicode_FromWideChar(buf, n2);
index bc9b04a27b5387368fd052635f422578964552e5..3c7d70083a98934d65abc82eb6c9f63c4e102d11 100644 (file)
@@ -210,7 +210,7 @@ random_seed(RandomObject *self, PyObject *args)
     PyObject *masklower = NULL;
     PyObject *thirtytwo = NULL;
     PyObject *n = NULL;
-    unsigned long *key = NULL;
+    unsigned long *new_key, *key = NULL;
     unsigned long keymax;               /* # of allocated slots in key */
     unsigned long keyused;              /* # of used slots in key */
     int err;
@@ -287,10 +287,11 @@ random_seed(RandomObject *self, PyObject *args)
                 PyErr_NoMemory();
                 goto Done;
             }
-            key = (unsigned long *)PyMem_Realloc(key,
+            new_key = (unsigned long *)PyMem_Realloc(key,
                                     bigger * sizeof(*key));
-            if (key == NULL)
+            if (new_key == NULL)
                 goto Done;
+            key = new_key;
             keymax = bigger;
         }
         assert(keyused < keymax);
index ed79165770b4a7930d466667972b46504f96291d..5c982f5c29357c97ad37226ff5e874f7b0047fe5 100644 (file)
@@ -526,13 +526,16 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
             /* Hangul Decomposition adds three characters in
                a single step, so we need atleast that much room. */
             if (space < 3) {
+                Py_UCS4 *new_output;
                 osize += 10;
                 space += 10;
-                output = PyMem_Realloc(output, osize*sizeof(Py_UCS4));
-                if (output == NULL) {
+                new_output = PyMem_Realloc(output, osize*sizeof(Py_UCS4));
+                if (new_output == NULL) {
+                    PyMem_Free(output);
                     PyErr_NoMemory();
                     return NULL;
                 }
+                output = new_output;
             }
             /* Hangul Decomposition. */
             if (SBase <= code && code < (SBase+SCount)) {
index 62085562e3da6c85d9bb9d5b92cae26ca1b9e440..929385fe28d75cf1900f78962c6f739ed98db1d4 100644 (file)
@@ -663,11 +663,13 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
             f = free_list;
             free_list = free_list->f_back;
             if (Py_SIZE(f) < extras) {
-                f = PyObject_GC_Resize(PyFrameObject, f, extras);
-                if (f == NULL) {
+                PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras);
+                if (new_f == NULL) {
+                    PyObject_GC_Del(f);
                     Py_DECREF(builtins);
                     return NULL;
                 }
+                f = new_f;
             }
             _Py_NewReference((PyObject *)f);
         }
index 874e80e1dd6a8b5197a66af3492805ae66720688..9e9378ef11db73dc75a71d931bd0780273810cbf 100644 (file)
@@ -8343,13 +8343,15 @@ charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize,
                                Py_ssize_t requiredsize)
 {
     Py_ssize_t oldsize = *psize;
+    Py_UCS4 *new_outobj;
     if (requiredsize > oldsize) {
         /* exponentially overallocate to minimize reallocations */
         if (requiredsize < 2 * oldsize)
             requiredsize = 2 * oldsize;
-        *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
-        if (*outobj == 0)
+        new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
+        if (new_outobj == 0)
             return -1;
+        *outobj = new_outobj;
         *psize = requiredsize;
     }
     return 0;