Issue #14909: A number of places were using PyMem_Realloc() apis and

PyObject_GC_Resize() with incorrect error handling.  In case of errors,
the original object would be leaked.  This checkin fixes those cases.
parent 155dba6d
...@@ -257,11 +257,12 @@ PyLocale_strxfrm(PyObject* self, PyObject* args) ...@@ -257,11 +257,12 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
n2 = wcsxfrm(buf, s, n1); n2 = wcsxfrm(buf, s, n1);
if (n2 >= (size_t)n1) { if (n2 >= (size_t)n1) {
/* more space needed */ /* more space needed */
buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
if (!buf) { if (!new_buf) {
PyErr_NoMemory(); PyErr_NoMemory();
goto exit; goto exit;
} }
buf = new_buf;
n2 = wcsxfrm(buf, s, n2+1); n2 = wcsxfrm(buf, s, n2+1);
} }
result = PyUnicode_FromWideChar(buf, n2); result = PyUnicode_FromWideChar(buf, n2);
......
...@@ -210,7 +210,7 @@ random_seed(RandomObject *self, PyObject *args) ...@@ -210,7 +210,7 @@ random_seed(RandomObject *self, PyObject *args)
PyObject *masklower = NULL; PyObject *masklower = NULL;
PyObject *thirtytwo = NULL; PyObject *thirtytwo = NULL;
PyObject *n = NULL; PyObject *n = NULL;
unsigned long *key = NULL; unsigned long *new_key, *key = NULL;
unsigned long keymax; /* # of allocated slots in key */ unsigned long keymax; /* # of allocated slots in key */
unsigned long keyused; /* # of used slots in key */ unsigned long keyused; /* # of used slots in key */
int err; int err;
...@@ -287,10 +287,11 @@ random_seed(RandomObject *self, PyObject *args) ...@@ -287,10 +287,11 @@ random_seed(RandomObject *self, PyObject *args)
PyErr_NoMemory(); PyErr_NoMemory();
goto Done; goto Done;
} }
key = (unsigned long *)PyMem_Realloc(key, new_key = (unsigned long *)PyMem_Realloc(key,
bigger * sizeof(*key)); bigger * sizeof(*key));
if (key == NULL) if (new_key == NULL)
goto Done; goto Done;
key = new_key;
keymax = bigger; keymax = bigger;
} }
assert(keyused < keymax); assert(keyused < keymax);
......
...@@ -526,13 +526,16 @@ nfd_nfkd(PyObject *self, PyObject *input, int k) ...@@ -526,13 +526,16 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
/* Hangul Decomposition adds three characters in /* Hangul Decomposition adds three characters in
a single step, so we need atleast that much room. */ a single step, so we need atleast that much room. */
if (space < 3) { if (space < 3) {
Py_UCS4 *new_output;
osize += 10; osize += 10;
space += 10; space += 10;
output = PyMem_Realloc(output, osize*sizeof(Py_UCS4)); new_output = PyMem_Realloc(output, osize*sizeof(Py_UCS4));
if (output == NULL) { if (new_output == NULL) {
PyMem_Free(output);
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
output = new_output;
} }
/* Hangul Decomposition. */ /* Hangul Decomposition. */
if (SBase <= code && code < (SBase+SCount)) { if (SBase <= code && code < (SBase+SCount)) {
......
...@@ -663,11 +663,13 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, ...@@ -663,11 +663,13 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
f = free_list; f = free_list;
free_list = free_list->f_back; free_list = free_list->f_back;
if (Py_SIZE(f) < extras) { if (Py_SIZE(f) < extras) {
f = PyObject_GC_Resize(PyFrameObject, f, extras); PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras);
if (f == NULL) { if (new_f == NULL) {
PyObject_GC_Del(f);
Py_DECREF(builtins); Py_DECREF(builtins);
return NULL; return NULL;
} }
f = new_f;
} }
_Py_NewReference((PyObject *)f); _Py_NewReference((PyObject *)f);
} }
......
...@@ -8343,13 +8343,15 @@ charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, ...@@ -8343,13 +8343,15 @@ charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize,
Py_ssize_t requiredsize) Py_ssize_t requiredsize)
{ {
Py_ssize_t oldsize = *psize; Py_ssize_t oldsize = *psize;
Py_UCS4 *new_outobj;
if (requiredsize > oldsize) { if (requiredsize > oldsize) {
/* exponentially overallocate to minimize reallocations */ /* exponentially overallocate to minimize reallocations */
if (requiredsize < 2 * oldsize) if (requiredsize < 2 * oldsize)
requiredsize = 2 * oldsize; requiredsize = 2 * oldsize;
*outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
if (*outobj == 0) if (new_outobj == 0)
return -1; return -1;
*outobj = new_outobj;
*psize = requiredsize; *psize = requiredsize;
} }
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment