mirror of
https://github.com/python/cpython.git
synced 2025-08-23 10:16:01 +00:00
bpo-36819: Fix crashes in built-in encoders with weird error handlers (GH-28593)
If the error handler returns position less or equal than the starting position of non-encodable characters, most of built-in encoders didn't properly re-size the output buffer. This led to out-of-bounds writes, and segfaults.
This commit is contained in:
parent
614420df97
commit
18b07d773e
4 changed files with 222 additions and 32 deletions
|
@ -387,8 +387,19 @@ STRINGLIB(utf8_encoder)(_PyBytesWriter *writer,
|
|||
if (!rep)
|
||||
goto error;
|
||||
|
||||
/* subtract preallocated bytes */
|
||||
writer->min_size -= max_char_size * (newpos - startpos);
|
||||
if (newpos < startpos) {
|
||||
writer->overallocate = 1;
|
||||
p = _PyBytesWriter_Prepare(writer, p,
|
||||
max_char_size * (startpos - newpos));
|
||||
if (p == NULL)
|
||||
goto error;
|
||||
}
|
||||
else {
|
||||
/* subtract preallocated bytes */
|
||||
writer->min_size -= max_char_size * (newpos - startpos);
|
||||
/* Only overallocate the buffer if it's not the last write */
|
||||
writer->overallocate = (newpos < size);
|
||||
}
|
||||
|
||||
if (PyBytes_Check(rep)) {
|
||||
p = _PyBytesWriter_WriteBytes(writer, p,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue