Skip to content

Commit 894263b

Browse files
bpo-24214: Fixed the UTF-8 and UTF-16 incremental decoders. (GH-14304)
* The UTF-8 incremental decoders fails now fast if encounter a sequence that can't be handled by the error handler. * The UTF-16 incremental decoders with the surrogatepass error handler decodes now a lone low surrogate with final=False.
1 parent 9fe42b4 commit 894263b

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

Lib/test/test_codecs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,18 @@ def test_lone_surrogates(self):
429429
def test_incremental_surrogatepass(self):
430430
# Test incremental decoder for surrogatepass handler:
431431
# see issue #24214
432+
# High surrogate
432433
data = '\uD901'.encode(self.encoding, 'surrogatepass')
433434
for i in range(1, len(data)):
434435
dec = codecs.getincrementaldecoder(self.encoding)('surrogatepass')
435436
self.assertEqual(dec.decode(data[:i]), '')
436437
self.assertEqual(dec.decode(data[i:], True), '\uD901')
438+
# Low surrogate
439+
data = '\uDC02'.encode(self.encoding, 'surrogatepass')
440+
for i in range(1, len(data)):
441+
dec = codecs.getincrementaldecoder(self.encoding)('surrogatepass')
442+
self.assertEqual(dec.decode(data[:i]), '')
443+
self.assertEqual(dec.decode(data[i:]), '\uDC02')
437444

438445

439446
class UTF32Test(ReadTest, unittest.TestCase):
@@ -874,6 +881,23 @@ def test_surrogatepass_handler(self):
874881
with self.assertRaises(UnicodeDecodeError):
875882
b"abc\xed\xa0z".decode(self.encoding, "surrogatepass")
876883

884+
def test_incremental_errors(self):
885+
# Test that the incremental decoder can fail with final=False.
886+
# See issue #24214
887+
cases = [b'\x80', b'\xBF', b'\xC0', b'\xC1', b'\xF5', b'\xF6', b'\xFF']
888+
for prefix in (b'\xC2', b'\xDF', b'\xE0', b'\xE0\xA0', b'\xEF',
889+
b'\xEF\xBF', b'\xF0', b'\xF0\x90', b'\xF0\x90\x80',
890+
b'\xF4', b'\xF4\x8F', b'\xF4\x8F\xBF'):
891+
for suffix in b'\x7F', b'\xC0':
892+
cases.append(prefix + suffix)
893+
cases.extend((b'\xE0\x80', b'\xE0\x9F', b'\xED\xA0\x80',
894+
b'\xED\xBF\xBF', b'\xF0\x80', b'\xF0\x8F', b'\xF4\x90'))
895+
896+
for data in cases:
897+
with self.subTest(data=data):
898+
dec = codecs.getincrementaldecoder(self.encoding)()
899+
self.assertRaises(UnicodeDecodeError, dec.decode, data)
900+
877901

878902
class UTF7Test(ReadTest, unittest.TestCase):
879903
encoding = "utf-7"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improved support of the surrogatepass error handler in the UTF-8 and UTF-16
2+
incremental decoders.

Objects/stringlib/codecs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ STRINGLIB(utf8_decode)(const char **inptr, const char *end,
207207
goto InvalidContinuation1;
208208
} else if (ch == 0xF4 && ch2 >= 0x90) {
209209
/* invalid sequence
210-
\xF4\x90\x80\80- -- 110000- overflow */
210+
\xF4\x90\x80\x80- -- 110000- overflow */
211211
goto InvalidContinuation1;
212212
}
213213
if (!IS_CONTINUATION_BYTE(ch3)) {
@@ -573,10 +573,10 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
573573
}
574574

575575
/* UTF-16 code pair: */
576-
if (q >= e)
577-
goto UnexpectedEnd;
578576
if (!Py_UNICODE_IS_HIGH_SURROGATE(ch))
579577
goto IllegalEncoding;
578+
if (q >= e)
579+
goto UnexpectedEnd;
580580
ch2 = (q[ihi] << 8) | q[ilo];
581581
q += 2;
582582
if (!Py_UNICODE_IS_LOW_SURROGATE(ch2))

Objects/unicodeobject.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4945,11 +4945,15 @@ unicode_decode_utf8(const char *s, Py_ssize_t size,
49454945
endinpos = startinpos + 1;
49464946
break;
49474947
case 2:
4948-
case 3:
4949-
case 4:
4950-
if (s == end || consumed) {
4948+
if (consumed && (unsigned char)s[0] == 0xED && end - s == 2
4949+
&& (unsigned char)s[1] >= 0xA0 && (unsigned char)s[1] <= 0xBF)
4950+
{
4951+
/* Truncated surrogate code in range D800-DFFF */
49514952
goto End;
49524953
}
4954+
/* fall through */
4955+
case 3:
4956+
case 4:
49534957
errmsg = "invalid continuation byte";
49544958
startinpos = s - starts;
49554959
endinpos = startinpos + ch - 1;

0 commit comments

Comments
 (0)