[Python-checkins] cpython: Fix error handling in resize_compact()

victor.stinner python-checkins at python.org
Mon Dec 12 01:25:52 CET 2011


http://hg.python.org/cpython/rev/21d7f5fdd34c
changeset:   73932:21d7f5fdd34c
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Dec 11 20:04:56 2011 +0100
summary:
  Fix error handling in resize_compact()

files:
  Objects/unicodeobject.c |  20 ++++++++++++--------
  1 files changed, 12 insertions(+), 8 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -630,6 +630,7 @@
     Py_ssize_t struct_size;
     Py_ssize_t new_size;
     int share_wstr;
+    PyObject *new_unicode;
 
     assert(PyUnicode_IS_READY(unicode));
     char_size = PyUnicode_KIND(unicode);
@@ -639,22 +640,25 @@
         struct_size = sizeof(PyCompactUnicodeObject);
     share_wstr = _PyUnicode_SHARE_WSTR(unicode);
 
+    if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) {
+        Py_DECREF(unicode);
+        PyErr_NoMemory();
+        return NULL;
+    }
+    new_size = (struct_size + (length + 1) * char_size);
+
     _Py_DEC_REFTOTAL;
     _Py_ForgetReference(unicode);
 
-    if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) {
-        PyErr_NoMemory();
-        return NULL;
-    }
-    new_size = (struct_size + (length + 1) * char_size);
-
-    unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size);
-    if (unicode == NULL) {
+    new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size);
+    if (new_unicode == NULL) {
         PyObject_Del(unicode);
         PyErr_NoMemory();
         return NULL;
     }
+    unicode = new_unicode;
     _Py_NewReference(unicode);
+
     _PyUnicode_LENGTH(unicode) = length;
     if (share_wstr) {
         _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode);

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


More information about the Python-checkins mailing list