]> granicus.if.org Git - python/commitdiff
bpo-24214: Fixed the UTF-8 incremental decoder. (GH-12603)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 30 Mar 2019 06:23:38 +0000 (08:23 +0200)
committerGitHub <noreply@github.com>
Sat, 30 Mar 2019 06:23:38 +0000 (08:23 +0200)
The bug occurred when the encoded surrogate character is passed
to the incremental decoder in two chunks.

Lib/test/test_codecs.py
Misc/NEWS.d/next/Core and Builtins/2019-03-28-15-22-45.bpo-24214.tZ6lYU.rst [new file with mode: 0644]
Objects/unicodeobject.c

index 331449397e37837c9d8d8c1b8c6878d79af6ad8c..05843c54bd5f9a3ee220b5b1791273e074d18b1c 100644 (file)
@@ -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 (file)
index 0000000..abb2759
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed support of the surrogatepass error handler in the UTF-8 incremental
+decoder.
index 8ab3943e61b29f78d0bd5d9970dfcd17410cc2ba..c0b345be7e8d3ba75f2627a7b5d3df5e106b82c8 100644 (file)
@@ -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;