]> granicus.if.org Git - python/commitdiff
Issue #17223: Fix PyUnicode_FromUnicode() on Windows (16-bit wchar_t type)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 5 Mar 2013 23:41:50 +0000 (00:41 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 5 Mar 2013 23:41:50 +0000 (00:41 +0100)
to reject invalid UTF-16 surrogate.

Misc/NEWS
Objects/unicodeobject.c

index a0dd2f60f077aaa3fdf3bd52e9854594bd237054..d858cd2e66fe905d96f6b1e035932972add8c78a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
 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.
 
index 21756550391cb289cdcc85c824c5916e3e86a8a5..00a6a36fcd25be937586536d51b72e4281fdd194 100644 (file)
@@ -1384,13 +1384,18 @@ find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end,
 
     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