[Python-checkins] cpython: Check for PyUnicode_CopyCharacters() failure

victor.stinner python-checkins at python.org
Wed Sep 28 22:08:37 CEST 2011


http://hg.python.org/cpython/rev/c4dcb68e0711
changeset:   72493:c4dcb68e0711
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Wed Sep 28 21:39:17 2011 +0200
summary:
  Check for PyUnicode_CopyCharacters() failure

files:
  Objects/unicodeobject.c |  120 ++++++++++++++++++---------
  Python/compile.c        |   10 +-
  2 files changed, 87 insertions(+), 43 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1784,9 +1784,10 @@
                 (void) va_arg(vargs, char *);
                 size = PyUnicode_GET_LENGTH(*callresult);
                 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
-                PyUnicode_CopyCharacters((PyObject*)string, i,
-                                         *callresult, 0,
-                                         size);
+                if (PyUnicode_CopyCharacters((PyObject*)string, i,
+                                             *callresult, 0,
+                                             size) < 0)
+                    goto fail;
                 i += size;
                 /* We're done with the unicode()/repr() => forget it */
                 Py_DECREF(*callresult);
@@ -1800,9 +1801,10 @@
                 Py_ssize_t size;
                 assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
                 size = PyUnicode_GET_LENGTH(obj);
-                PyUnicode_CopyCharacters((PyObject*)string, i,
-                                         obj, 0,
-                                         size);
+                if (PyUnicode_CopyCharacters((PyObject*)string, i,
+                                             obj, 0,
+                                             size) < 0)
+                    goto fail;
                 i += size;
                 break;
             }
@@ -1814,17 +1816,19 @@
                 if (obj) {
                     size = PyUnicode_GET_LENGTH(obj);
                     assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
-                    PyUnicode_CopyCharacters((PyObject*)string, i,
-                                             obj, 0,
-                                             size);
+                    if (PyUnicode_CopyCharacters((PyObject*)string, i,
+                                                 obj, 0,
+                                                 size) < 0)
+                        goto fail;
                     i += size;
                 } else {
                     size = PyUnicode_GET_LENGTH(*callresult);
                     assert(PyUnicode_KIND(*callresult) <=
                            PyUnicode_KIND(string));
-                    PyUnicode_CopyCharacters((PyObject*)string, i,
-                                             *callresult,
-                                             0, size);
+                    if (PyUnicode_CopyCharacters((PyObject*)string, i,
+                                                 *callresult,
+                                                 0, size) < 0)
+                        goto fail;
                     i += size;
                     Py_DECREF(*callresult);
                 }
@@ -1838,9 +1842,10 @@
                 /* unused, since we already have the result */
                 (void) va_arg(vargs, PyObject *);
                 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
-                PyUnicode_CopyCharacters((PyObject*)string, i,
-                                         *callresult, 0,
-                                         PyUnicode_GET_LENGTH(*callresult));
+                if (PyUnicode_CopyCharacters((PyObject*)string, i,
+                                             *callresult, 0,
+                                             PyUnicode_GET_LENGTH(*callresult)) < 0)
+                    goto fail;
                 i += PyUnicode_GET_LENGTH(*callresult);
                 /* We're done with the unicode()/repr() => forget it */
                 Py_DECREF(*callresult);
@@ -8141,8 +8146,7 @@
     else {
         /* In case the maximum character changed, we need to
            convert the string to the new category. */
-        PyObject *v = PyUnicode_New(
-                             PyUnicode_GET_LENGTH(self), maxchar_new);
+        PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new);
         if (v == NULL) {
             Py_DECREF(u);
             return NULL;
@@ -8151,12 +8155,25 @@
             /* If the maxchar increased so that the kind changed, not all
                characters are representable anymore and we need to fix the
                string again. This only happens in very few cases. */
-            PyUnicode_CopyCharacters(v, 0, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
+            if (PyUnicode_CopyCharacters(v, 0, 
+                                         (PyObject*)self, 0, 
+                                         PyUnicode_GET_LENGTH(self)) < 0)
+            {
+                Py_DECREF(u);
+                return NULL;
+            }
             maxchar_old = fixfct((PyUnicodeObject*)v);
             assert(maxchar_old > 0 && maxchar_old <= maxchar_new);
         }
-        else
-            PyUnicode_CopyCharacters(v, 0, u, 0, PyUnicode_GET_LENGTH(self));
+        else {
+            if (PyUnicode_CopyCharacters(v, 0, 
+                                         u, 0, 
+                                         PyUnicode_GET_LENGTH(self)) < 0)
+            {
+                Py_DECREF(u);
+                return NULL;
+            }
+        }
 
         Py_DECREF(u);
         return v;
@@ -8455,12 +8472,14 @@
         itemlen = PyUnicode_GET_LENGTH(item);
         /* Copy item, and maybe the separator. */
         if (i) {
-            PyUnicode_CopyCharacters(res, res_offset,
-                                     sep, 0, seplen);
+            if (PyUnicode_CopyCharacters(res, res_offset,
+                                         sep, 0, seplen) < 0)
+                goto onError;
             res_offset += seplen;
         }
-        PyUnicode_CopyCharacters(res, res_offset,
-                                 item, 0, itemlen);
+        if (PyUnicode_CopyCharacters(res, res_offset,
+                                     item, 0, itemlen) < 0)
+            goto onError;
         res_offset += itemlen;
     }
     assert(res_offset == PyUnicode_GET_LENGTH(res));
@@ -8508,6 +8527,8 @@
 {
     PyObject *u;
     Py_UCS4 maxchar;
+    int kind;
+    void *data;
 
     if (left < 0)
         left = 0;
@@ -8528,14 +8549,21 @@
     if (fill > maxchar)
         maxchar = fill;
     u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
-    if (u) {
-        int kind = PyUnicode_KIND(u);
-        void *data = PyUnicode_DATA(u);
-        if (left)
-            FILL(kind, data, fill, 0, left);
-        if (right)
-            FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
-        PyUnicode_CopyCharacters(u, left, (PyObject*)self, 0, _PyUnicode_LENGTH(self));
+    if (!u)
+        return NULL;
+
+    kind = PyUnicode_KIND(u);
+    data = PyUnicode_DATA(u);
+    if (left)
+        FILL(kind, data, fill, 0, left);
+    if (right)
+        FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
+    if (PyUnicode_CopyCharacters(u, left, 
+                                 (PyObject*)self, 0, 
+                                 _PyUnicode_LENGTH(self)) < 0)
+    {
+        Py_DECREF(u);
+        return NULL;
     }
 
     return (PyUnicodeObject*)u;
@@ -8821,8 +8849,12 @@
             u = PyUnicode_New(slen, maxchar);
             if (!u)
                 goto error;
-            PyUnicode_CopyCharacters(u, 0,
-                                     (PyObject*)self, 0, slen);
+            if (PyUnicode_CopyCharacters(u, 0,
+                                         (PyObject*)self, 0, slen) < 0)
+            {
+                Py_DECREF(u);
+                return NULL;
+            }
             rkind = PyUnicode_KIND(u);
             for (i = 0; i < PyUnicode_GET_LENGTH(u); i++)
                 if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) {
@@ -9437,8 +9469,7 @@
         goto onError;
 
     maxchar = PyUnicode_MAX_CHAR_VALUE(u);
-    if (PyUnicode_MAX_CHAR_VALUE(v) > maxchar)
-        maxchar = PyUnicode_MAX_CHAR_VALUE(v);
+    maxchar = PY_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v));
 
     /* Concat the two Unicode strings */
     w = PyUnicode_New(
@@ -9446,9 +9477,12 @@
         maxchar);
     if (w == NULL)
         goto onError;
-    PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u));
-    PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), v, 0,
-                             PyUnicode_GET_LENGTH(v));
+    if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0)
+        goto onError;
+    if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), 
+                                 v, 0,
+                                 PyUnicode_GET_LENGTH(v)) < 0)
+        goto onError;
     Py_DECREF(u);
     Py_DECREF(v);
     return w;
@@ -10396,8 +10430,12 @@
     unicode = PyUnicode_New(len, maxchar);
     if (unicode == NULL)
         return NULL;
-    PyUnicode_CopyCharacters(unicode, 0,
-                             (PyObject*)self, start, len);
+    if (PyUnicode_CopyCharacters(unicode, 0,
+                                 (PyObject*)self, start, len) < 0)
+    {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return unicode;
 }
 
diff --git a/Python/compile.c b/Python/compile.c
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -245,8 +245,14 @@
         return 0;
     /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
     PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
-    PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen);
-    PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen);
+    if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
+        Py_DECREF(result);
+        return NULL;
+    }
+    if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
+        Py_DECREF(result);
+        return NULL;
+    }
     return result;
 }
 

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


More information about the Python-checkins mailing list