[Python-checkins] cpython: Issue #17223: Fix PyUnicode_FromUnicode() on Windows (16-bit wchar_t type)

victor.stinner python-checkins at python.org
Wed Mar 6 00:42:22 CET 2013


http://hg.python.org/cpython/rev/b9f7b1bf36aa
changeset:   82502:b9f7b1bf36aa
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Mar 06 00:41:50 2013 +0100
summary:
  Issue #17223: Fix PyUnicode_FromUnicode() on Windows (16-bit wchar_t type)
to reject invalid UTF-16 surrogate.

files:
  Misc/NEWS               |   3 +++
  Objects/unicodeobject.c |  19 ++++++++++++-------
  2 files changed, 15 insertions(+), 7 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #17223: Fix PyUnicode_FromUnicode() on Windows (16-bit wchar_t type)
+  to reject invalid UTF-16 surrogate.
+
 - Issue #17032: The "global" in the "NameError: global name 'x' is not defined"
   error message has been removed.  Patch by Ram Rachum.
 
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1384,13 +1384,18 @@
 
     for (iter = begin; iter < end; ) {
 #if SIZEOF_WCHAR_T == 2
-        if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])
-            && (iter+1) < end
-            && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
-        {
-            ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
-            ++(*num_surrogates);
-            iter += 2;
+        if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0])) {
+            if ((iter+1) < end
+                && Py_UNICODE_IS_LOW_SURROGATE(iter[1]))
+            {
+                ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]);
+                ++(*num_surrogates);
+                iter += 2;
+            }
+            else {
+                PyErr_SetString(PyExc_ValueError, "illegal UTF-16 surrogate");
+                return -1;
+            }
         }
         else
 #endif

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list