[Python-checkins] cpython: Issue #19437: Fix _PyUnicode_New() (constructor of legacy string), set all
victor.stinner
python-checkins at python.org
Tue Oct 29 19:45:59 CET 2013
http://hg.python.org/cpython/rev/22ce0b7b1eaf
changeset: 86744:22ce0b7b1eaf
user: Victor Stinner <victor.stinner at gmail.com>
date: Tue Oct 29 19:31:43 2013 +0100
summary:
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.
files:
Objects/unicodeobject.c | 35 +++++++++++++++-------------
1 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -896,22 +896,7 @@
if (unicode == NULL)
return NULL;
new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
- _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
- if (!_PyUnicode_WSTR(unicode)) {
- Py_DECREF(unicode);
- PyErr_NoMemory();
- return NULL;
- }
-
- /* Initialize the first element to guard against cases where
- * the caller fails before initializing str -- unicode_resize()
- * reads str[0], and the Keep-Alive optimization can keep memory
- * allocated for str alive across a call to unicode_dealloc(unicode).
- * We don't want unicode_resize to read uninitialized memory in
- * that case.
- */
- _PyUnicode_WSTR(unicode)[0] = 0;
- _PyUnicode_WSTR(unicode)[length] = 0;
+
_PyUnicode_WSTR_LENGTH(unicode) = length;
_PyUnicode_HASH(unicode) = -1;
_PyUnicode_STATE(unicode).interned = 0;
@@ -923,6 +908,24 @@
_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);
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ /* Initialize the first element to guard against cases where
+ * the caller fails before initializing str -- unicode_resize()
+ * reads str[0], and the Keep-Alive optimization can keep memory
+ * allocated for str alive across a call to unicode_dealloc(unicode).
+ * We don't want unicode_resize to read uninitialized memory in
+ * that case.
+ */
+ _PyUnicode_WSTR(unicode)[0] = 0;
+ _PyUnicode_WSTR(unicode)[length] = 0;
+
assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0));
return unicode;
}
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list