FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-159739: Bound encoding-name lookahead in surrogatepass (GH-159785) by miss-islington · Pull Request #159911 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_codeccallbacks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,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 @@ -1191,7 +1192,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

Back | FazBrowse Home | New Git URL