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

gh-158925: Fix heap overflow in the UTF-16 encoder by christianaurichzm · Pull Request #159007 · python/cpython · GitHub

Repository navigation

Open
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
15 changes: 15 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 @@ -1100,6 +1100,21 @@ def test_encodehelper_bug36819(self):
self.assertEqual(encoded[0].decode(), "abcdx" * 51)
self.assertEqual(encoded[1], len(input))

def test_encode_backward_non_bmp(self):
# Characters re-encoded after a backward jump can take more
# space than a single code unit.
handler = RepeatedPosReturn("x")
codecs.register_error("test.backward_non_bmp", handler.handle)

input = "\U0001f40d" * 10 + "\udc80"
for enc in ("utf-8", "utf-16", "utf-16-le", "utf-16-be",
"utf-32", "utf-32-le", "utf-32-be"):
with self.subTest(encoding=enc):
handler.count = 50
encoded = input.encode(enc, "test.backward_non_bmp")
self.assertEqual(encoded.decode(enc),
("\U0001f40d" * 10 + "x") * 51)

def test_translatehelper(self):
# enhance coverage of:
# Objects/unicodeobject.c::unicode_encode_call_errorhandler()
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,3 @@
Fix a crash when encoding a string with non-BMP characters to UTF-16 with
an error handler that returns a position before the error. Patch by
Christian Aurich Zanettini Martins.
4 changes: 4 additions & 0 deletions Objects/unicodeobject.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 @@ -6478,6 +6478,10 @@ _PyUnicode_EncodeUTF16(PyObject *str,
}
}
moreunits += pos - newpos;
if (newpos < pos && kind == PyUnicode_4BYTE_KIND) {
/* re-encoded characters may need a surrogate pair */
moreunits += pos - newpos;
}
pos = newpos;

/* two bytes are reserved for each surrogate */
Expand Down
Loading

Back | FazBrowse Home | New Git URL