From 7a465cb5ee7e298cae626ace1fc3e7d97df79f2e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 30 Mar 2019 08:23:38 +0200 Subject: bpo-24214: Fixed the UTF-8 incremental decoder. (GH-12603) The bug occurred when the encoded surrogate character is passed to the incremental decoder in two chunks. --- Lib/test/test_codecs.py | 9 +++++++++ .../Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst | 2 ++ Objects/unicodeobject.c | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 3314493..05843c5 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -406,6 +406,15 @@ class ReadTest(MixInCheckStateHandling): self.assertEqual(test_sequence.decode(self.encoding, "backslashreplace"), before + backslashreplace + after) + def test_incremental_surrogatepass(self): + # Test incremental decoder for surrogatepass handler: + # see issue #24214 + data = '\uD901'.encode(self.encoding, 'surrogatepass') + for i in range(1, len(data)): + dec = codecs.getincrementaldecoder(self.encoding)('surrogatepass') + self.assertEqual(dec.decode(data[:i]), '') + self.assertEqual(dec.decode(data[i:], True), '\uD901') + class UTF32Test(ReadTest, unittest.TestCase): encoding = "utf-32" diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst b/Misc/NEWS.d/next/Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst new file mode 100644 index 0000000..abb2759 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst @@ -0,0 +1,2 @@ +Fixed support of the surrogatepass error handler in the UTF-8 incremental +decoder. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 8ab3943..c0b345b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -4883,6 +4883,9 @@ PyUnicode_DecodeUTF8Stateful(const char *s, case 2: case 3: case 4: + if (s == end || consumed) { + goto End; + } errmsg = "invalid continuation byte"; startinpos = s - starts; endinpos = startinpos + ch - 1; -- cgit v0.12