Skip to content

Commit 4e7457b

Browse files
authored
bpo-29990: Fix range checking in GB18030 decoder (python#1509)
1 parent 228da42 commit 4e7457b

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/test_codecencodings_cn.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class Test_GB18030(test_multibytecodec_support.TestBase, unittest.TestCase):
4646
("abc\x80\x80\xc1\xc4", "ignore", u"abc\u804a"),
4747
("abc\x84\x39\x84\x39\xc1\xc4", "replace", u"abc\ufffd\u804a"),
4848
(u"\u30fb", "strict", "\x819\xa79"),
49+
# issue29990
50+
("\xff\x30\x81\x30", "strict", None),
51+
("\x81\x30\xff\x30", "strict", None),
52+
("abc\x81\x39\xff\x39\xc1\xc4", "replace", u"abc\ufffd\u804a"),
53+
("abc\xab\x36\xff\x30def", "replace", u'abc\ufffddef'),
54+
("abc\xbf\x38\xff\x32\xc1\xc4", "ignore", u"abc\u804a"),
4955
)
5056
has_iso10646 = True
5157

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Extension Modules
4242
Library
4343
-------
4444

45+
- bpo-29990: Fix range checking in GB18030 decoder. Original patch by Ma Lin.
46+
4547
- bpo-30243: Removed the __init__ methods of _json's scanner and encoder.
4648
Misusing them could cause memory leaks or crashes. Now scanner and encoder
4749
objects are completely initialized in the __new__ methods.

Modules/cjkcodecs/_codecs_cn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ DECODER(gb18030)
266266
REQUIRE_INBUF(4)
267267
c3 = IN3;
268268
c4 = IN4;
269-
if (c < 0x81 || c3 < 0x81 || c4 < 0x30 || c4 > 0x39)
269+
if (c < 0x81 || c > 0xFE ||
270+
c3 < 0x81 || c3 > 0xFE ||
271+
c4 < 0x30 || c4 > 0x39)
270272
return 4;
271273
c -= 0x81; c2 -= 0x30;
272274
c3 -= 0x81; c4 -= 0x30;

0 commit comments

Comments
 (0)