From a0257e160a8298afa8d683660d87aba03b2c7eb8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 13 Sep 2026 02:29:20 +0200 Subject: [PATCH] gh-156939: Update UTF-8 encoder for PyBytesWriter changes Don't modify directly the writer size, call PyBytesWriter_GrowAndUpdatePointer() instead. --- Objects/stringlib/codecs.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h index 6ea7ef1c9a92bf..aa3de5e34eaaae 100644 --- a/Objects/stringlib/codecs.h +++ b/Objects/stringlib/codecs.h @@ -350,8 +350,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, break; case _Py_ERROR_BACKSLASHREPLACE: - /* subtract preallocated bytes */ - writer->size -= max_char_size * (endpos - startpos); p = backslashreplace(writer, p, unicode, startpos, endpos); if (p == NULL) @@ -360,8 +358,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, break; case _Py_ERROR_XMLCHARREFREPLACE: - /* subtract preallocated bytes */ - writer->size -= max_char_size * (endpos - startpos); p = xmlcharrefreplace(writer, p, unicode, startpos, endpos); if (p == NULL) @@ -400,10 +396,15 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, } } else { - /* subtract preallocated bytes */ - writer->size -= max_char_size * (newpos - startpos); /* Only overallocate the buffer if it's not the last write */ writer->overallocate = (newpos < size); + + /* subtract preallocated bytes */ + Py_ssize_t prealloc = max_char_size * (newpos - startpos); + p = PyBytesWriter_GrowAndUpdatePointer(writer, -prealloc, p); + if (p == NULL) { + goto error; + } } const char *rep_str;