[Python-checkins] cpython: Close #14085: remove assertions from PyUnicode_WRITE macro

victor.stinner python-checkins at python.org
Sun Mar 4 01:38:52 CET 2012


http://hg.python.org/cpython/rev/ba0bd949ddf5
changeset:   75378:ba0bd949ddf5
parent:      75376:58eef400866e
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sun Mar 04 01:34:37 2012 +0100
summary:
  Close #14085: remove assertions from PyUnicode_WRITE macro

Add checks in PyUnicode_WriteChar() and convert PyUnicode_New() assertion to a
test raising a Python exception.

files:
  Include/unicodeobject.h |   3 ---
  Objects/unicodeobject.c |  11 ++++++++++-
  2 files changed, 10 insertions(+), 4 deletions(-)


diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -499,17 +499,14 @@
     do { \
         switch ((kind)) { \
         case PyUnicode_1BYTE_KIND: { \
-            assert(value <= 0xff); \
             ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
             break; \
         } \
         case PyUnicode_2BYTE_KIND: { \
-            assert(value <= 0xffff); \
             ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
             break; \
         } \
         default: { \
-            assert(value <= 0x10ffff); \
             assert((kind) == PyUnicode_4BYTE_KIND); \
             ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
         } \
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -998,7 +998,11 @@
             is_sharing = 1;
     }
     else {
-        assert(maxchar <= MAX_UNICODE);
+        if (maxchar > MAX_UNICODE) {
+            PyErr_SetString(PyExc_SystemError,
+                            "invalid maximum character passed to PyUnicode_New");
+            return NULL;
+        }
         kind_state = PyUnicode_4BYTE_KIND;
         char_size = 4;
         if (sizeof(wchar_t) == 4)
@@ -3931,6 +3935,7 @@
 int
 PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
 {
+    Py_UCS4 maxchar;
     if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
         PyErr_BadArgument();
         return -1;
@@ -3942,6 +3947,10 @@
     }
     if (unicode_check_modifiable(unicode))
         return -1;
+    if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
+        PyErr_SetString(PyExc_ValueError, "character out of range");
+        return -1;
+    }
     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
                     index, ch);
     return 0;

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


More information about the Python-checkins mailing list