summaryrefslogtreecommitdiffstats
path: root/Modules/_ctypes
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-09-29 10:37:16 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-09-29 10:37:16 (GMT)
commit4c2e4fa242d2567e6f0bb5d112cc4e3c5d085b68 (patch)
treeafe3f954be7ba7e8028512959c236b208d8bcb55 /Modules/_ctypes
parentb29047876dcd889efab5b4adcba7ddb70ad4c3aa (diff)
downloadcpython-4c2e4fa242d2567e6f0bb5d112cc4e3c5d085b68.zip
cpython-4c2e4fa242d2567e6f0bb5d112cc4e3c5d085b68.tar.gz
cpython-4c2e4fa242d2567e6f0bb5d112cc4e3c5d085b68.tar.bz2
Issue #9979: Use PyUnicode_AsWideCharString() in _ctypes module
* Convert unicode to wide character string before creating the PyCapsule object * Catch integer overflow * Avoid useless memset() * Prepare the support of surrogates
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r--Modules/_ctypes/callproc.c15
-rw-r--r--Modules/_ctypes/cfield.c14
2 files changed, 5 insertions, 24 deletions
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 81d3294..ce2acb5 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -665,24 +665,15 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
pa->keep = obj;
return 0;
#else
- int size = PyUnicode_GET_SIZE(obj);
pa->ffi_type = &ffi_type_pointer;
- size += 1; /* terminating NUL */
- size *= sizeof(wchar_t);
- pa->value.p = PyMem_Malloc(size);
- if (!pa->value.p) {
- PyErr_NoMemory();
+ pa->value.p = PyUnicode_AsWideCharString((PyUnicodeObject *)obj, NULL);
+ if (pa->value.p == NULL)
return -1;
- }
- memset(pa->value.p, 0, size);
pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
if (!pa->keep) {
PyMem_Free(pa->value.p);
return -1;
}
- if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)obj,
- pa->value.p, PyUnicode_GET_SIZE(obj)))
- return -1;
return 0;
#endif
}
@@ -1147,7 +1138,7 @@ PyObject *_ctypes_callproc(PPROC pProc,
}
for (i = 0; i < argcount; ++i) {
atypes[i] = args[i].ffi_type;
- if (atypes[i]->type == FFI_TYPE_STRUCT
+ if (atypes[i]->type == FFI_TYPE_STRUCT
#ifdef _WIN64
&& atypes[i]->size <= sizeof(void *)
#endif
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 3d7d0bc..ba60deb 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1433,15 +1433,11 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
PyObject *keep;
wchar_t *buffer;
- int size = PyUnicode_GET_SIZE(value);
- size += 1; /* terminating NUL */
- size *= sizeof(wchar_t);
- buffer = (wchar_t *)PyMem_Malloc(size);
+ buffer = PyUnicode_AsWideCharString((PyUnicodeObject *)value, NULL);
if (!buffer) {
Py_DECREF(value);
- return PyErr_NoMemory();
+ return NULL;
}
- memset(buffer, 0, size);
keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
if (!keep) {
Py_DECREF(value);
@@ -1449,12 +1445,6 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
return NULL;
}
*(wchar_t **)ptr = (wchar_t *)buffer;
- if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value,
- buffer, PyUnicode_GET_SIZE(value))) {
- Py_DECREF(value);
- Py_DECREF(keep);
- return NULL;
- }
Py_DECREF(value);
return keep;
}