Commits

Anonymous committed d3c1947

Backport from trunk r52223:

Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault
when encoding non-BMP unicode characters. (Submitted by Ray Chason)

Comments (0)

Files changed (3)

Lib/test/test_multibytecodec.py

 
 from test import test_support
 from test import test_multibytecodec_support
-import unittest, StringIO, codecs
+import unittest, StringIO, codecs, sys
 
 class Test_StreamWriter(unittest.TestCase):
     if len(u'\U00012345') == 2: # UCS2
             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_StreamWriter))
 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.
 

Modules/cjkcodecs/_codecs_iso2022.c

 {
 	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;
 }
 
 {
 	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;
 }
 {
 	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;
 }
 {
 	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;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.