Skip to content

Commit f5f7870

Browse files
authored
bpo-29990: Fix range checking in GB18030 decoder (#1495) (#1508)
When decoding a 4-byte GB18030 sequence, the first and third byte cannot exceed 0xFE.
1 parent c8faabc commit f5f7870

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_codecencodings_cn.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class Test_GB18030(multibytecodec_support.TestBase, unittest.TestCase):
4949
(b"abc\x84\x32\x80\x80def", "replace", 'abc\ufffd2\ufffd\ufffddef'),
5050
(b"abc\x81\x30\x81\x30def", "strict", 'abc\x80def'),
5151
(b"abc\x86\x30\x81\x30def", "replace", 'abc\ufffd0\ufffd0def'),
52+
# issue29990
53+
(b"\xff\x30\x81\x30", "strict", None),
54+
(b"\x81\x30\xff\x30", "strict", None),
55+
(b"abc\x81\x39\xff\x39\xc1\xc4", "replace", "abc\ufffd\x39\ufffd\x39\u804a"),
56+
(b"abc\xab\x36\xff\x30def", "replace", 'abc\ufffd\x36\ufffd\x30def'),
57+
(b"abc\xbf\x38\xff\x32\xc1\xc4", "ignore", "abc\x38\x32\u804a"),
5258
)
5359
has_iso10646 = True
5460

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Extension Modules
4949
Library
5050
-------
5151

52+
- bpo-29990: Fix range checking in GB18030 decoder. Original patch by Ma Lin.
53+
5254
- Revert bpo-26293 for zipfile breakage. See also bpo-29094.
5355

5456
- bpo-30243: Removed the __init__ methods of _json's scanner and encoder.

Modules/cjkcodecs/_codecs_cn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ DECODER(gb18030)
279279
REQUIRE_INBUF(4);
280280
c3 = INBYTE3;
281281
c4 = INBYTE4;
282-
if (c < 0x81 || c3 < 0x81 || c4 < 0x30 || c4 > 0x39)
282+
if (c < 0x81 || c > 0xFE ||
283+
c3 < 0x81 || c3 > 0xFE ||
284+
c4 < 0x30 || c4 > 0x39)
283285
return 1;
284286
c -= 0x81; c2 -= 0x30;
285287
c3 -= 0x81; c4 -= 0x30;

0 commit comments

Comments
 (0)