gh-121849: Fix PyUnicodeWriter_WriteSubstring() crash if len=0 (#121896)

Do nothing if start=end.
This commit is contained in:
Victor Stinner 2024-07-17 10:26:05 +02:00 committed by GitHub
parent 5d98a4d266
commit bfdbeac355
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 13 deletions

View file

@ -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);