diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h index 6ea7ef1c9a92bf4..aa3de5e34eaaae0 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;