diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-11 19:04:56 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-11 19:04:56 (GMT) |
commit | 84def3774d2079ea2a812e0220507ff0e27247e7 (patch) | |
tree | 1a11f75e529fdd56b0901cc1c35ce7f1091f40c5 /Objects/unicodeobject.c | |
parent | 8bbe788deb774d066e8d185b1af04af3b4537dfc (diff) | |
download | cpython-84def3774d2079ea2a812e0220507ff0e27247e7.zip cpython-84def3774d2079ea2a812e0220507ff0e27247e7.tar.gz cpython-84def3774d2079ea2a812e0220507ff0e27247e7.tar.bz2 |
Fix error handling in resize_compact()
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 9670ae8..2846bd2 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -630,6 +630,7 @@ resize_compact(PyObject *unicode, Py_ssize_t length) 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 @@ resize_compact(PyObject *unicode, Py_ssize_t length) struct_size = sizeof(PyCompactUnicodeObject); share_wstr = _PyUnicode_SHARE_WSTR(unicode); - _Py_DEC_REFTOTAL; - _Py_ForgetReference(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); - unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); - if (unicode == NULL) { + _Py_DEC_REFTOTAL; + _Py_ForgetReference(unicode); + + 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); |