@@ -350,8 +350,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
350350 break ;
351351
352352 case _Py_ERROR_BACKSLASHREPLACE :
353- /* subtract preallocated bytes */
354- writer -> size -= max_char_size * (endpos - startpos );
355353 p = backslashreplace (writer , p ,
356354 unicode , startpos , endpos );
357355 if (p == NULL )
@@ -360,8 +358,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
360358 break ;
361359
362360 case _Py_ERROR_XMLCHARREFREPLACE :
363- /* subtract preallocated bytes */
364- writer -> size -= max_char_size * (endpos - startpos );
365361 p = xmlcharrefreplace (writer , p ,
366362 unicode , startpos , endpos );
367363 if (p == NULL )
@@ -400,10 +396,15 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
400396 }
401397 }
402398 else {
403- /* subtract preallocated bytes */
404- writer -> size -= max_char_size * (newpos - startpos );
405399 /* Only overallocate the buffer if it's not the last write */
406400 writer -> overallocate = (newpos < size );
401+
402+ /* subtract preallocated bytes */
403+ Py_ssize_t prealloc = max_char_size * (newpos - startpos );
404+ p = PyBytesWriter_GrowAndUpdatePointer (writer , - prealloc , p );
405+ if (p == NULL ) {
406+ goto error ;
407+ }
407408 }
408409
409410 const char * rep_str ;
0 commit comments