[Python-checkins] cpython: ctypes check for PyUnicode_GET_SIZE() failure

victor.stinner python-checkins at python.org
Mon Nov 21 02:14:57 CET 2011


http://hg.python.org/cpython/rev/fbf76c5c5c66
changeset:   73652:fbf76c5c5c66
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Nov 21 02:11:26 2011 +0100
summary:
  ctypes check for PyUnicode_GET_SIZE() failure

files:
  Modules/_ctypes/_ctypes.c |   8 +++++++-
  Modules/_ctypes/cfield.c  |  14 +++++++++-----
  2 files changed, 16 insertions(+), 6 deletions(-)


diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1142,6 +1142,8 @@
 WCharArray_set_value(CDataObject *self, PyObject *value)
 {
     Py_ssize_t result = 0;
+    Py_UNICODE *wstr;
+    Py_ssize_t len;
 
     if (value == NULL) {
         PyErr_SetString(PyExc_TypeError,
@@ -1155,7 +1157,11 @@
         return -1;
     } else
         Py_INCREF(value);
-    if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) {
+
+    wstr = PyUnicode_AsUnicodeAndSize(value, &len);
+    if (wstr == NULL)
+        return -1;
+    if ((unsigned)len > self->b_size/sizeof(wchar_t)) {
         PyErr_SetString(PyExc_ValueError,
                         "string too long");
         result = -1;
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1259,6 +1259,7 @@
 static PyObject *
 U_set(void *ptr, PyObject *value, Py_ssize_t length)
 {
+    Py_UNICODE *wstr;
     Py_ssize_t size;
 
     /* It's easier to calculate in characters than in bytes */
@@ -1271,7 +1272,10 @@
         return NULL;
     } else
         Py_INCREF(value);
-    size = PyUnicode_GET_SIZE(value);
+
+    wstr = PyUnicode_AsUnicodeAndSize(value, &size);
+    if (wstr == NULL)
+        return NULL;
     if (size > length) {
         PyErr_Format(PyExc_ValueError,
                      "string too long (%zd, maximum length %zd)",
@@ -1471,15 +1475,15 @@
 
     /* create a BSTR from value */
     if (value) {
-        Py_ssize_t size = PyUnicode_GET_SIZE(value);
         wchar_t* wvalue;
+        Py_ssize_t size;
+        wvalue = PyUnicode_AsUnicodeAndSize(value, &size);
+        if (wvalue == NULL)
+            return NULL;
         if ((unsigned) size != size) {
             PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
             return NULL;
         }
-        wvalue = PyUnicode_AsUnicode(value);
-        if (wvalue == NULL)
-            return NULL;
         bstr = SysAllocStringLen(wvalue, (unsigned)size);
         Py_DECREF(value);
     } else

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


More information about the Python-checkins mailing list