]> granicus.if.org Git - python/commitdiff
bpo-29990: Fix range checking in GB18030 decoder (#1495) (#1508)
authorXiang Zhang <angwerzx@126.com>
Tue, 9 May 2017 04:17:09 +0000 (12:17 +0800)
committerGitHub <noreply@github.com>
Tue, 9 May 2017 04:17:09 +0000 (12:17 +0800)
When decoding a 4-byte GB18030 sequence, the first and third byte cannot exceed 0xFE.

Lib/test/test_codecencodings_cn.py
Misc/NEWS
Modules/cjkcodecs/_codecs_cn.c

index d0e3a15d1623706a37710ee5e6431404eecd5ba4..f135bb26e7b044de52ad2098b4f3c6f20cdfb5f7 100644 (file)
@@ -49,6 +49,12 @@ class Test_GB18030(multibytecodec_support.TestBase, unittest.TestCase):
         (b"abc\x84\x32\x80\x80def", "replace", 'abc\ufffd2\ufffd\ufffddef'),
         (b"abc\x81\x30\x81\x30def", "strict", 'abc\x80def'),
         (b"abc\x86\x30\x81\x30def", "replace", 'abc\ufffd0\ufffd0def'),
+        # issue29990
+        (b"\xff\x30\x81\x30", "strict", None),
+        (b"\x81\x30\xff\x30", "strict", None),
+        (b"abc\x81\x39\xff\x39\xc1\xc4", "replace", "abc\ufffd\x39\ufffd\x39\u804a"),
+        (b"abc\xab\x36\xff\x30def", "replace", 'abc\ufffd\x36\ufffd\x30def'),
+        (b"abc\xbf\x38\xff\x32\xc1\xc4", "ignore",  "abc\x38\x32\u804a"),
     )
     has_iso10646 = True
 
index 1ec6a0976048874e50f49494b8ccc2dd0678d847..06e464ffc89d803341a211372fb6098d57cfba44 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -49,6 +49,8 @@ Extension Modules
 Library
 -------
 
+- bpo-29990: Fix range checking in GB18030 decoder.  Original patch by Ma Lin.
+
 - Revert bpo-26293 for zipfile breakage. See also bpo-29094.
 
 - bpo-30243: Removed the __init__ methods of _json's scanner and encoder.
index 1a070f2f393219de4ec69bf4a14ba081c6b436ee..bda175c55d13239ae7e41366e240f2b192fb54d6 100644 (file)
@@ -279,7 +279,9 @@ DECODER(gb18030)
             REQUIRE_INBUF(4);
             c3 = INBYTE3;
             c4 = INBYTE4;
-            if (c < 0x81 || c3 < 0x81 || c4 < 0x30 || c4 > 0x39)
+            if (c  < 0x81 || c  > 0xFE ||
+                c3 < 0x81 || c3 > 0xFE ||
+                c4 < 0x30 || c4 > 0x39)
                 return 1;
             c -= 0x81;  c2 -= 0x30;
             c3 -= 0x81; c4 -= 0x30;