Skip to content

Commit 2cd6d4b

Browse files
authored
gh-156939: Update UTF-8 encoder for PyBytesWriter changes (#157383)
Don't modify directly the writer size, call PyBytesWriter_GrowAndUpdatePointer() instead.
1 parent 12a1de1 commit 2cd6d4b

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

Objects/stringlib/codecs.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)