summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-10-29 18:31:43 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-10-29 18:31:43 (GMT)
commit68b674c9d403906e91bb5a294449d90f2d747b8c (patch)
tree4b9b5b498a92b84467dda7e684fc2a8d2933da04 /Objects
parent0b0c86717807bf86ba5d1d7d332529ad08179934 (diff)
downloadcpython-68b674c9d403906e91bb5a294449d90f2d747b8c.zip
cpython-68b674c9d403906e91bb5a294449d90f2d747b8c.tar.gz
cpython-68b674c9d403906e91bb5a294449d90f2d747b8c.tar.bz2
Issue #19437: Fix _PyUnicode_New() (constructor of legacy string), set all
attributes before checking for error. The destructor expects all attributes to be set. It is now safe to call Py_DECREF(unicode) in the constructor.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index a7ea9c8..208e5e3 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -896,6 +896,19 @@ _PyUnicode_New(Py_ssize_t length)
if (unicode == NULL)
return NULL;
new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
+
+ _PyUnicode_WSTR_LENGTH(unicode) = length;
+ _PyUnicode_HASH(unicode) = -1;
+ _PyUnicode_STATE(unicode).interned = 0;
+ _PyUnicode_STATE(unicode).kind = 0;
+ _PyUnicode_STATE(unicode).compact = 0;
+ _PyUnicode_STATE(unicode).ready = 0;
+ _PyUnicode_STATE(unicode).ascii = 0;
+ _PyUnicode_DATA_ANY(unicode) = NULL;
+ _PyUnicode_LENGTH(unicode) = 0;
+ _PyUnicode_UTF8(unicode) = NULL;
+ _PyUnicode_UTF8_LENGTH(unicode) = 0;
+
_PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
if (!_PyUnicode_WSTR(unicode)) {
Py_DECREF(unicode);
@@ -912,17 +925,7 @@ _PyUnicode_New(Py_ssize_t length)
*/
_PyUnicode_WSTR(unicode)[0] = 0;
_PyUnicode_WSTR(unicode)[length] = 0;
- _PyUnicode_WSTR_LENGTH(unicode) = length;
- _PyUnicode_HASH(unicode) = -1;
- _PyUnicode_STATE(unicode).interned = 0;
- _PyUnicode_STATE(unicode).kind = 0;
- _PyUnicode_STATE(unicode).compact = 0;
- _PyUnicode_STATE(unicode).ready = 0;
- _PyUnicode_STATE(unicode).ascii = 0;
- _PyUnicode_DATA_ANY(unicode) = NULL;
- _PyUnicode_LENGTH(unicode) = 0;
- _PyUnicode_UTF8(unicode) = NULL;
- _PyUnicode_UTF8_LENGTH(unicode) = 0;
+
assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0));
return unicode;
}