Skip to content

Commit bbd3aa8

Browse files
Issue #23321: Fixed a crash in str.decode() when error handler returned
replacment string longer than mailformed input data.
2 parents 07985ef + 7e4b905 commit bbd3aa8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #23321: Fixed a crash in str.decode() when error handler returned
14+
replacment string longer than mailformed input data.
15+
1316
- Issue #22286: The "backslashreplace" error handlers now works with
1417
decoding and translating.
1518

Objects/unicodeobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4155,9 +4155,13 @@ unicode_decode_call_errorhandler_writer(
41554155
if (PyUnicode_READY(repunicode) < 0)
41564156
goto onError;
41574157
replen = PyUnicode_GET_LENGTH(repunicode);
4158-
writer->min_length += replen;
4159-
if (replen > 1)
4158+
if (replen > 1) {
4159+
writer->min_length += replen - 1;
41604160
writer->overallocate = 1;
4161+
if (_PyUnicodeWriter_Prepare(writer, writer->min_length,
4162+
PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1)
4163+
goto onError;
4164+
}
41614165
if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1)
41624166
goto onError;
41634167

0 commit comments

Comments
 (0)