Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_codeccallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,18 @@ def test_badandgoodsurrogatepassexceptions(self):
(s[:1], 1 + n)
)

def test_surrogatepass_truncated_encoding_name(self):
handler = codecs.lookup_error('surrogatepass')
for width in (16, 32):
encoding = f'utf-{width}-'
data = (0xd800).to_bytes(width // 8, 'little')
for exc in (UnicodeEncodeError(encoding, '\ud800', 0, 1, 'test'),
UnicodeDecodeError(encoding, data, 0, len(data), 'test')):
with self.subTest(encoding=encoding, error=type(exc).__name__):
with self.assertRaises(type(exc)) as cm:
handler(exc)
self.assertIs(cm.exception, exc)

def test_badhandlerresults(self):
results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid reading past the end of an encoding name when the ``surrogatepass``
error handler receives a truncated UTF-16 or UTF-32 name.
6 changes: 4 additions & 2 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,8 @@ get_standard_encoding_impl(const char *encoding, int *bytelength)
}
if (*encoding == '-' || *encoding == '_' )
encoding++;
if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
if (encoding[0] != '\0' &&
Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
if (Py_TOLOWER(encoding[0]) == 'b')
return ENC_UTF16BE;
if (Py_TOLOWER(encoding[0]) == 'l')
Expand All @@ -1192,7 +1193,8 @@ get_standard_encoding_impl(const char *encoding, int *bytelength)
}
if (*encoding == '-' || *encoding == '_' )
encoding++;
if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
if (encoding[0] != '\0' &&
Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
if (Py_TOLOWER(encoding[0]) == 'b')
return ENC_UTF32BE;
if (Py_TOLOWER(encoding[0]) == 'l')
Expand Down
Loading