Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68b674c

Browse files
committed
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.
1 parent 0b0c867 commit 68b674c

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

Objects/unicodeobject.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,19 @@ _PyUnicode_New(Py_ssize_t length)
896896
if (unicode == NULL)
897897
return NULL;
898898
new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
899+
900+
_PyUnicode_WSTR_LENGTH(unicode) = length;
901+
_PyUnicode_HASH(unicode) = -1;
902+
_PyUnicode_STATE(unicode).interned = 0;
903+
_PyUnicode_STATE(unicode).kind = 0;
904+
_PyUnicode_STATE(unicode).compact = 0;
905+
_PyUnicode_STATE(unicode).ready = 0;
906+
_PyUnicode_STATE(unicode).ascii = 0;
907+
_PyUnicode_DATA_ANY(unicode) = NULL;
908+
_PyUnicode_LENGTH(unicode) = 0;
909+
_PyUnicode_UTF8(unicode) = NULL;
910+
_PyUnicode_UTF8_LENGTH(unicode) = 0;
911+
899912
_PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
900913
if (!_PyUnicode_WSTR(unicode)) {
901914
Py_DECREF(unicode);
@@ -912,17 +925,7 @@ _PyUnicode_New(Py_ssize_t length)
912925
*/
913926
_PyUnicode_WSTR(unicode)[0] = 0;
914927
_PyUnicode_WSTR(unicode)[length] = 0;
915-
_PyUnicode_WSTR_LENGTH(unicode) = length;
916-
_PyUnicode_HASH(unicode) = -1;
917-
_PyUnicode_STATE(unicode).interned = 0;
918-
_PyUnicode_STATE(unicode).kind = 0;
919-
_PyUnicode_STATE(unicode).compact = 0;
920-
_PyUnicode_STATE(unicode).ready = 0;
921-
_PyUnicode_STATE(unicode).ascii = 0;
922-
_PyUnicode_DATA_ANY(unicode) = NULL;
923-
_PyUnicode_LENGTH(unicode) = 0;
924-
_PyUnicode_UTF8(unicode) = NULL;
925-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
928+
926929
assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0));
927930
return unicode;
928931
}

0 commit comments

Comments
 (0)