mirror of
https://github.com/python/cpython.git
synced 2025-08-27 12:16:04 +00:00
gh-121849: Fix PyUnicodeWriter_WriteSubstring() crash if len=0 (#121896)
Do nothing if start=end.
This commit is contained in:
parent
5d98a4d266
commit
bfdbeac355
2 changed files with 18 additions and 13 deletions
|
@ -13637,27 +13637,28 @@ int
|
|||
_PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str,
|
||||
Py_ssize_t start, Py_ssize_t end)
|
||||
{
|
||||
Py_UCS4 maxchar;
|
||||
Py_ssize_t len;
|
||||
|
||||
assert(0 <= start);
|
||||
assert(end <= PyUnicode_GET_LENGTH(str));
|
||||
assert(start <= end);
|
||||
|
||||
if (end == 0)
|
||||
return 0;
|
||||
|
||||
if (start == 0 && end == PyUnicode_GET_LENGTH(str))
|
||||
return _PyUnicodeWriter_WriteStr(writer, str);
|
||||
|
||||
if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar)
|
||||
maxchar = _PyUnicode_FindMaxChar(str, start, end);
|
||||
else
|
||||
maxchar = writer->maxchar;
|
||||
len = end - start;
|
||||
Py_ssize_t len = end - start;
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0)
|
||||
Py_UCS4 maxchar;
|
||||
if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) {
|
||||
maxchar = _PyUnicode_FindMaxChar(str, start, end);
|
||||
}
|
||||
else {
|
||||
maxchar = writer->maxchar;
|
||||
}
|
||||
if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
_PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
|
||||
str, start, len);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue