(original) (raw)

changeset: 74236:d5cda62d0f8c user: Benjamin Peterson benjamin@python.org date: Mon Jan 02 09:00:30 2012 -0600 files: Objects/unicodeobject.c description: fix some possible refleaks from PyUnicode_READY error conditions diff -r bea974c88faf -r d5cda62d0f8c Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sun Jan 01 16:04:29 2012 -0600 +++ b/Objects/unicodeobject.c Mon Jan 02 09:00:30 2012 -0600 @@ -9132,10 +9132,15 @@ Py_ssize_t len1, len2; str_obj = PyUnicode_FromObject(str); - if (!str_obj || PyUnicode_READY(str_obj) == -1) + if (!str_obj) return -1; sub_obj = PyUnicode_FromObject(substr); - if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { + if (!sub_obj) { + Py_DECREF(str_obj); + return -1; + } + if (PyUnicode_READY(substr) == -1 || PyUnicode_READY(str_obj) == -1) { + Py_DECREF(substr); Py_DECREF(str_obj); return -1; } @@ -9215,10 +9220,15 @@ Py_ssize_t result; str = PyUnicode_FromObject(str); - if (!str || PyUnicode_READY(str) == -1) + if (!str) return -2; sub = PyUnicode_FromObject(sub); - if (!sub || PyUnicode_READY(sub) == -1) { + if (!sub) { + Py_DECREF(str); + return -2; + } + if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { + Py_DECREF(sub); Py_DECREF(str); return -2; } @@ -9857,8 +9867,12 @@ PyObject *list; string = PyUnicode_FromObject(string); - if (string == NULL || PyUnicode_READY(string) == -1) - return NULL; + if (string == NULL) + return NULL; + if (PyUnicode_READY(string) == -1) { + Py_DECREF(string); + return NULL; + } switch (PyUnicode_KIND(string)) { case PyUnicode_1BYTE_KIND: @@ -10650,14 +10664,16 @@ element->ob_type->tp_name); return -1; } - if (PyUnicode_READY(sub) == -1) - return -1; str = PyUnicode_FromObject(container); - if (!str || PyUnicode_READY(str) == -1) { + if (!str) { Py_DECREF(sub); return -1; } + if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { + Py_DECREF(sub); + Py_DECREF(str); + } kind1 = PyUnicode_KIND(str); kind2 = PyUnicode_KIND(sub); @@ -11936,20 +11952,25 @@ PyObject *result; self = PyUnicode_FromObject(obj); - if (self == NULL || PyUnicode_READY(self) == -1) + if (self == NULL) return NULL; str1 = PyUnicode_FromObject(subobj); - if (str1 == NULL || PyUnicode_READY(str1) == -1) { + if (str1 == NULL) { Py_DECREF(self); return NULL; } str2 = PyUnicode_FromObject(replobj); - if (str2 == NULL || PyUnicode_READY(str2)) { + if (str2 == NULL) { Py_DECREF(self); Py_DECREF(str1); return NULL; } - result = replace(self, str1, str2, maxcount); + if (PyUnicode_READY(self) == -1 || + PyUnicode_READY(str1) == -1 || + PyUnicode_READY(str2) == -1) + result = NULL; + else + result = replace(self, str1, str2, maxcount); Py_DECREF(self); Py_DECREF(str1); Py_DECREF(str2); @@ -11973,18 +11994,20 @@ if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) return NULL; - if (!PyUnicode_READY(self) == -1) + if (PyUnicode_READY(self) == -1) return NULL; str1 = PyUnicode_FromObject(str1); - if (str1 == NULL || PyUnicode_READY(str1) == -1) + if (str1 == NULL) return NULL; str2 = PyUnicode_FromObject(str2); - if (str2 == NULL || PyUnicode_READY(str2) == -1) { + if (str2 == NULL) { Py_DECREF(str1); return NULL; } - - result = replace(self, str1, str2, maxcount); + if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) + result = NULL; + else + result = replace(self, str1, str2, maxcount); Py_DECREF(str1); Py_DECREF(str2); @@ -12299,10 +12322,15 @@ Py_ssize_t len1, len2; str_obj = PyUnicode_FromObject(str_in); - if (!str_obj || PyUnicode_READY(str_obj) == -1) + if (!str_obj) return NULL; sep_obj = PyUnicode_FromObject(sep_in); - if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { + if (!sep_obj) { + Py_DECREF(str_obj); + return NULL; + } + if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { + Py_DECREF(sep_obj); Py_DECREF(str_obj); return NULL; } @@ -13227,8 +13255,10 @@ return NULL; } uformat = PyUnicode_FromObject(format); - if (uformat == NULL || PyUnicode_READY(uformat) == -1) - return NULL; + if (uformat == NULL) + return NULL; + if (PyUnicode_READY(uformat) == -1) + Py_DECREF(uformat); if (_PyAccu_Init(&acc)) goto onError; fmt = PyUnicode_DATA(uformat); @@ -13729,8 +13759,10 @@ if (unicode == NULL) return NULL; assert(_PyUnicode_CHECK(unicode)); - if (PyUnicode_READY(unicode)) - return NULL; + if (PyUnicode_READY(unicode)) { + Py_DECREF(unicode); + return NULL; + } self = type->tp_alloc(type, 0); if (self == NULL) { /benjamin@python.org