Skip to content

ctypes: remove use of legacy unicode API #12340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,6 @@ static int
WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{
Py_ssize_t result = 0;
Py_UNICODE *wstr;
Py_ssize_t len;

if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
Expand All @@ -1309,12 +1307,14 @@ WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored
} else
Py_INCREF(value);

wstr = PyUnicode_AsUnicodeAndSize(value, &len);
if (wstr == NULL)
Py_ssize_t len = PyUnicode_AsWideChar(value, NULL, 0);
if (len < 0) {
return -1;
if ((size_t)len > self->b_size/sizeof(wchar_t)) {
PyErr_SetString(PyExc_ValueError,
"string too long");
}
// PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
// when it is called with NULL.
if (((size_t)len-1) > self->b_size/sizeof(wchar_t)) {
PyErr_SetString(PyExc_ValueError, "string too long");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth providing in the exception message the provided length and what the max length actually is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe yes. But this file has similar codes which are not relating to wchar_t APIs. ("bytes string too long").
I want to keep this pull request simple.

result = -1;
goto done;
}
Expand Down
20 changes: 12 additions & 8 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,6 @@ U_get(void *ptr, Py_ssize_t size)
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 */
length /= sizeof(wchar_t);

Expand All @@ -1242,9 +1239,14 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
return NULL;
}

wstr = PyUnicode_AsUnicodeAndSize(value, &size);
if (wstr == NULL)
Py_ssize_t size = PyUnicode_AsWideChar(value, NULL, 0);
if (size < 0) {
return NULL;
}
// PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
// when it is called with NULL.
size--;
assert(size >= 0);
if (size > length) {
PyErr_Format(PyExc_ValueError,
"string too long (%zd, maximum length %zd)",
Expand Down Expand Up @@ -1421,16 +1423,18 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)

/* create a BSTR from value */
if (value) {
wchar_t* wvalue;
Py_ssize_t wsize;
wvalue = PyUnicode_AsUnicodeAndSize(value, &wsize);
if (wvalue == NULL)
wchar_t *wvalue = PyUnicode_AsWideCharString(value, &wsize);
if (wvalue == NULL) {
return NULL;
}
if ((unsigned) wsize != wsize) {
PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
PyMem_Free(wvalue);
return NULL;
}
bstr = SysAllocStringLen(wvalue, (unsigned)wsize);
PyMem_Free(wvalue);
} else
bstr = NULL;

Expand Down