Backport from trunk r52223:
authorHye-Shik Chang <hyeshik@gmail.com>
Sun, 8 Oct 2006 14:01:45 +0000 (14:01 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Sun, 8 Oct 2006 14:01:45 +0000 (14:01 +0000)
Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault
when encoding non-BMP unicode characters.  (Submitted by Ray Chason)

Lib/test/test_multibytecodec.py
Misc/NEWS
Modules/cjkcodecs/_codecs_iso2022.c

index 800456e23b8bcdda6d4604e1de13e306c5582c20..a8666d31ccd05d1be06b145e51dd89f95234a154 100644 (file)
@@ -208,6 +208,16 @@ class Test_ISO2022(unittest.TestCase):
             e = u'\u3406'.encode(encoding)
             self.failIf(filter(lambda x: x >= '\x80', e))
 
+    def test_bug1572832(self):
+        if sys.maxunicode >= 0x10000:
+            myunichr = unichr
+        else:
+            myunichr = lambda x: unichr(0xD7C0+(x>>10)) + unichr(0xDC00+(x&0x3FF))
+
+        for x in xrange(0x10000, 0x110000):
+            # Any ISO 2022 codec will cause the segfault
+            myunichr(x).encode('iso_2022_jp', 'ignore')
+
 def test_main():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(Test_MultibyteCodec))
index a390730c1a719f56ceafc5afe50a21b0e6500999..daa0add90049ab173da30681d39c1c9bfe43fe00 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -49,6 +49,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault
+  when encoding non-BMP unicode characters.
+
 - Bug #1556784: allow format strings longer than 127 characters in
   datetime's strftime function.
 
index 2a11e9a2747700da34e4984a4e5f7b6ff73e8213..55196a9ea4cdf449158a686f3db5cb3c5ea5451a 100644 (file)
@@ -592,9 +592,11 @@ ksx1001_encoder(const ucs4_t *data, Py_ssize_t *length)
 {
        DBCHAR coded;
        assert(*length == 1);
-       TRYMAP_ENC(cp949, coded, *data)
-               if (!(coded & 0x8000))
-                       return coded;
+       if (*data < 0x10000) {
+               TRYMAP_ENC(cp949, coded, *data)
+                       if (!(coded & 0x8000))
+                               return coded;
+       }
        return MAP_UNMAPPABLE;
 }
 
@@ -628,11 +630,13 @@ jisx0208_encoder(const ucs4_t *data, Py_ssize_t *length)
 {
        DBCHAR coded;
        assert(*length == 1);
-       if (*data == 0xff3c) /* F/W REVERSE SOLIDUS */
-               return 0x2140;
-       else TRYMAP_ENC(jisxcommon, coded, *data) {
-               if (!(coded & 0x8000))
-                       return coded;
+       if (*data < 0x10000) {
+               if (*data == 0xff3c) /* F/W REVERSE SOLIDUS */
+                       return 0x2140;
+               else TRYMAP_ENC(jisxcommon, coded, *data) {
+                       if (!(coded & 0x8000))
+                               return coded;
+               }
        }
        return MAP_UNMAPPABLE;
 }
@@ -665,9 +669,11 @@ jisx0212_encoder(const ucs4_t *data, Py_ssize_t *length)
 {
        DBCHAR coded;
        assert(*length == 1);
-       TRYMAP_ENC(jisxcommon, coded, *data) {
-               if (coded & 0x8000)
-                       return coded & 0x7fff;
+       if (*data < 0x10000) {
+               TRYMAP_ENC(jisxcommon, coded, *data) {
+                       if (coded & 0x8000)
+                               return coded & 0x7fff;
+               }
        }
        return MAP_UNMAPPABLE;
 }
@@ -970,9 +976,11 @@ gb2312_encoder(const ucs4_t *data, Py_ssize_t *length)
 {
        DBCHAR coded;
        assert(*length == 1);
-       TRYMAP_ENC(gbcommon, coded, *data) {
-               if (!(coded & 0x8000))
-                       return coded;
+       if (*data < 0x10000) {
+               TRYMAP_ENC(gbcommon, coded, *data) {
+                       if (!(coded & 0x8000))
+                               return coded;
+               }
        }
        return MAP_UNMAPPABLE;
 }