Skip to content

Commit 9da408d

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

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
@@ -317,6 +317,8 @@ Extension Modules
317317
Library
318318
-------
319319

320+
- bpo-29990: Fix range checking in GB18030 decoder. Original patch by Ma Lin.
321+
320322
- bpo-29979: rewrite cgi.parse_multipart, reusing the FieldStorage class and
321323
making its results consistent with those of FieldStorage for
322324
multipart/form-data requests. Patch by Pierre Quentel.

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)