Skip to content

Commit 76b274d

Browse files
committed
Use _PyUnicode_Copy in sanitize_isoformat_str
1 parent 121eb16 commit 76b274d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Modules/_datetimemodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,18 +4869,21 @@ _sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
48694869
// assumption that all valid strings can be encoded in UTF-8, this function
48704870
// replaces any surrogate character separators with `T`.
48714871
Py_ssize_t len = PyUnicode_GetLength(dtstr);
4872+
if (len < 0) {
4873+
return NULL;
4874+
}
4875+
48724876
*needs_decref = 0;
48734877
if (len <= 10 || !Py_UNICODE_IS_SURROGATE(PyUnicode_READ_CHAR(dtstr, 10))) {
48744878
return dtstr;
48754879
}
48764880

4877-
PyObject *str_out = PyUnicode_New(len, PyUnicode_MAX_CHAR_VALUE(dtstr));
4881+
PyObject *str_out = _PyUnicode_Copy(dtstr);
48784882
if (str_out == NULL) {
48794883
return NULL;
48804884
}
48814885

4882-
if (PyUnicode_CopyCharacters(str_out, 0, dtstr, 0, len) == -1 ||
4883-
PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) {
4886+
if (PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) {
48844887
Py_DECREF(str_out);
48854888
return NULL;
48864889
}

0 commit comments

Comments
 (0)