Skip to content

Commit 28a6dab

Browse files
committed
FinishWithSize() sets recheck_maxchar
1 parent cf24b61 commit 28a6dab

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

‎Include/internal/pycore_unicodeobject.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ _PyUnicodeWriter_CanWrite(_PyUnicodeWriter *writer)
132132
static inline void
133133
_PyUnicodeWriter_SetBuffer(_PyUnicodeWriter *writer, PyObject *buffer)
134134
{
135+
assert(writer->pos <= PyUnicode_GET_LENGTH(buffer));
136+
135137
// Py_DECREF() the previous buffer (if any)
136138
Py_XSETREF(writer->buffer, buffer);
137139
writer->data = PyUnicode_DATA(buffer);

‎Objects/unicode_writer.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,12 @@ _PyUnicodeWriter_FinishWithSize(_PyUnicodeWriter *writer, Py_ssize_t size)
643643
{
644644
assert(0 <= size);
645645
if (writer->buffer != NULL) {
646+
assert(size <= writer->pos);
646647
assert(size <= PyUnicode_GET_LENGTH(writer->buffer));
648+
if (size < writer->pos) {
649+
// Truncate the string: we may need to adjust the string kind
650+
writer->recheck_maxchar = 1;
651+
}
647652
}
648653
else {
649654
assert(size == 0);

0 commit comments

Comments
 (0)