Skip to content

Commit 72e1b61

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

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
@@ -48,6 +48,12 @@ class Test_GB18030(multibytecodec_support.TestBase, unittest.TestCase):
4848
(b"abc\x84\x32\x80\x80def", "replace", 'abc\ufffd2\ufffd\ufffddef'),
4949
(b"abc\x81\x30\x81\x30def", "strict", 'abc\x80def'),
5050
(b"abc\x86\x30\x81\x30def", "replace", 'abc\ufffd0\ufffd0def'),
51+
# issue29990
52+
(b"\xff\x30\x81\x30", "strict", None),
53+
(b"\x81\x30\xff\x30", "strict", None),
54+
(b"abc\x81\x39\xff\x39\xc1\xc4", "replace", "abc\ufffd\x39\ufffd\x39\u804a"),
55+
(b"abc\xab\x36\xff\x30def", "replace", 'abc\ufffd\x36\ufffd\x30def'),
56+
(b"abc\xbf\x38\xff\x32\xc1\xc4", "ignore", "abc\x38\x32\u804a"),
5157
)
5258
has_iso10646 = True
5359

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- bpo-29990: Fix range checking in GB18030 decoder. Original patch by Ma Lin.
40+
3941
- Revert bpo-26293 for zipfile breakage. See also bpo-29094.
4042

4143
- 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)