mirror of
https://github.com/python/cpython.git
synced 2025-10-03 21:55:41 +00:00
[3.6] bpo-30708: Check for null characters in PyUnicode_AsWideCharString(). (GH-2285) (#2443)
Raise a ValueError if the second argument is NULL and the wchar_t\*
string contains null characters..
(cherry picked from commit e613e6add5
)
This commit is contained in:
parent
35d2ca2b94
commit
0edffa3073
12 changed files with 50 additions and 20 deletions
|
@ -1063,6 +1063,12 @@ PyAPI_FUNC(wchar_t*) PyUnicode_AsWideCharString(
|
||||||
);
|
);
|
||||||
|
|
||||||
#ifndef Py_LIMITED_API
|
#ifndef Py_LIMITED_API
|
||||||
|
/* Similar to PyUnicode_AsWideCharString(unicode, NULL), but check if
|
||||||
|
the string contains null characters. */
|
||||||
|
PyAPI_FUNC(wchar_t*) _PyUnicode_AsWideCharString(
|
||||||
|
PyObject *unicode /* Unicode object */
|
||||||
|
);
|
||||||
|
|
||||||
PyAPI_FUNC(void*) _PyUnicode_AsKind(PyObject *s, unsigned int kind);
|
PyAPI_FUNC(void*) _PyUnicode_AsKind(PyObject *s, unsigned int kind);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@ class SlicesTestCase(unittest.TestCase):
|
||||||
dll.my_wcsdup.restype = POINTER(c_wchar)
|
dll.my_wcsdup.restype = POINTER(c_wchar)
|
||||||
dll.my_wcsdup.argtypes = POINTER(c_wchar),
|
dll.my_wcsdup.argtypes = POINTER(c_wchar),
|
||||||
dll.my_free.restype = None
|
dll.my_free.restype = None
|
||||||
res = dll.my_wcsdup(s)
|
res = dll.my_wcsdup(s[:-1])
|
||||||
self.assertEqual(res[:len(s)], s)
|
self.assertEqual(res[:len(s)], s)
|
||||||
self.assertEqual(res[:len(s):], s)
|
self.assertEqual(res[:len(s):], s)
|
||||||
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
|
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
|
||||||
|
@ -153,7 +153,7 @@ class SlicesTestCase(unittest.TestCase):
|
||||||
dll.my_wcsdup.restype = POINTER(c_long)
|
dll.my_wcsdup.restype = POINTER(c_long)
|
||||||
else:
|
else:
|
||||||
self.skipTest('Pointers to c_wchar are not supported')
|
self.skipTest('Pointers to c_wchar are not supported')
|
||||||
res = dll.my_wcsdup(s)
|
res = dll.my_wcsdup(s[:-1])
|
||||||
tmpl = list(range(ord("a"), ord("z")+1))
|
tmpl = list(range(ord("a"), ord("z")+1))
|
||||||
self.assertEqual(res[:len(s)-1], tmpl)
|
self.assertEqual(res[:len(s)-1], tmpl)
|
||||||
self.assertEqual(res[:len(s)-1:], tmpl)
|
self.assertEqual(res[:len(s)-1:], tmpl)
|
||||||
|
|
|
@ -668,7 +668,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
|
||||||
#ifdef CTYPES_UNICODE
|
#ifdef CTYPES_UNICODE
|
||||||
if (PyUnicode_Check(obj)) {
|
if (PyUnicode_Check(obj)) {
|
||||||
pa->ffi_type = &ffi_type_pointer;
|
pa->ffi_type = &ffi_type_pointer;
|
||||||
pa->value.p = PyUnicode_AsWideCharString(obj, NULL);
|
pa->value.p = _PyUnicode_AsWideCharString(obj);
|
||||||
if (pa->value.p == NULL)
|
if (pa->value.p == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
|
pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
|
||||||
|
|
|
@ -1372,7 +1372,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
|
||||||
|
|
||||||
/* We must create a wchar_t* buffer from the unicode object,
|
/* We must create a wchar_t* buffer from the unicode object,
|
||||||
and keep it alive */
|
and keep it alive */
|
||||||
buffer = PyUnicode_AsWideCharString(value, NULL);
|
buffer = _PyUnicode_AsWideCharString(value);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
return NULL;
|
return NULL;
|
||||||
keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
|
keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
|
||||||
|
|
|
@ -345,7 +345,7 @@ PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
|
||||||
if (PyUnicode_Check(obj)) {
|
if (PyUnicode_Check(obj)) {
|
||||||
#ifdef HAVE_NCURSESW
|
#ifdef HAVE_NCURSESW
|
||||||
assert (wstr != NULL);
|
assert (wstr != NULL);
|
||||||
*wstr = PyUnicode_AsWideCharString(obj, NULL);
|
*wstr = _PyUnicode_AsWideCharString(obj);
|
||||||
if (*wstr == NULL)
|
if (*wstr == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
return 2;
|
return 2;
|
||||||
|
|
|
@ -79,7 +79,7 @@ char _PyIO_get_console_type(PyObject *path_or_fd) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
decoded_wstr = PyUnicode_AsWideCharString(decoded, NULL);
|
decoded_wstr = _PyUnicode_AsWideCharString(decoded);
|
||||||
Py_CLEAR(decoded);
|
Py_CLEAR(decoded);
|
||||||
if (!decoded_wstr) {
|
if (!decoded_wstr) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
@ -311,8 +311,7 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
|
||||||
if (!d)
|
if (!d)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
Py_ssize_t length;
|
name = _PyUnicode_AsWideCharString(decodedname);
|
||||||
name = PyUnicode_AsWideCharString(decodedname, &length);
|
|
||||||
console_type = _PyIO_get_console_type(decodedname);
|
console_type = _PyIO_get_console_type(decodedname);
|
||||||
Py_CLEAR(decodedname);
|
Py_CLEAR(decodedname);
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
|
@ -322,12 +321,6 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
|
||||||
"Cannot open non-console file");
|
"Cannot open non-console file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wcslen(name) != length) {
|
|
||||||
PyMem_Free(name);
|
|
||||||
PyErr_SetString(PyExc_ValueError, "embedded null character");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s = mode;
|
s = mode;
|
||||||
|
|
|
@ -215,10 +215,10 @@ PyLocale_strcoll(PyObject* self, PyObject* args)
|
||||||
if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
|
if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
|
||||||
return NULL;
|
return NULL;
|
||||||
/* Convert the unicode strings to wchar[]. */
|
/* Convert the unicode strings to wchar[]. */
|
||||||
ws1 = PyUnicode_AsWideCharString(os1, NULL);
|
ws1 = _PyUnicode_AsWideCharString(os1);
|
||||||
if (ws1 == NULL)
|
if (ws1 == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
ws2 = PyUnicode_AsWideCharString(os2, NULL);
|
ws2 = _PyUnicode_AsWideCharString(os2);
|
||||||
if (ws2 == NULL)
|
if (ws2 == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
/* Collate the strings. */
|
/* Collate the strings. */
|
||||||
|
|
|
@ -3615,7 +3615,7 @@ PyInit__tkinter(void)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (str_path != NULL) {
|
if (str_path != NULL) {
|
||||||
wcs_path = PyUnicode_AsWideCharString(str_path, NULL);
|
wcs_path = _PyUnicode_AsWideCharString(str_path);
|
||||||
if (wcs_path == NULL) {
|
if (wcs_path == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1151,7 +1151,7 @@ ConnectPipe(OverlappedObject *self, PyObject *args)
|
||||||
if (!PyArg_ParseTuple(args, "U", &AddressObj))
|
if (!PyArg_ParseTuple(args, "U", &AddressObj))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
Address = PyUnicode_AsWideCharString(AddressObj, NULL);
|
Address = _PyUnicode_AsWideCharString(AddressObj);
|
||||||
if (Address == NULL)
|
if (Address == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
|
@ -607,7 +607,7 @@ time_strftime(PyObject *self, PyObject *args)
|
||||||
buf.tm_isdst = 1;
|
buf.tm_isdst = 1;
|
||||||
|
|
||||||
#ifdef HAVE_WCSFTIME
|
#ifdef HAVE_WCSFTIME
|
||||||
format = PyUnicode_AsWideCharString(format_arg, NULL);
|
format = _PyUnicode_AsWideCharString(format_arg);
|
||||||
if (format == NULL)
|
if (format == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
fmt = format;
|
fmt = format;
|
||||||
|
|
|
@ -3017,6 +3017,37 @@ PyUnicode_AsWideCharString(PyObject *unicode,
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wchar_t*
|
||||||
|
_PyUnicode_AsWideCharString(PyObject *unicode)
|
||||||
|
{
|
||||||
|
const wchar_t *wstr;
|
||||||
|
wchar_t *buffer;
|
||||||
|
Py_ssize_t buflen;
|
||||||
|
|
||||||
|
if (unicode == NULL) {
|
||||||
|
PyErr_BadInternalCall();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wstr = PyUnicode_AsUnicodeAndSize(unicode, &buflen);
|
||||||
|
if (wstr == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (wcslen(wstr) != (size_t)buflen) {
|
||||||
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"embedded null character");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = PyMem_NEW(wchar_t, buflen + 1);
|
||||||
|
if (buffer == NULL) {
|
||||||
|
PyErr_NoMemory();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(buffer, wstr, (buflen + 1) * sizeof(wchar_t));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* HAVE_WCHAR_H */
|
#endif /* HAVE_WCHAR_H */
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
|
|
|
@ -101,7 +101,7 @@ winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags)
|
||||||
Py_TYPE(sound)->tp_name);
|
Py_TYPE(sound)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
wsound = PyUnicode_AsWideCharString(sound, NULL);
|
wsound = _PyUnicode_AsWideCharString(sound);
|
||||||
if (wsound == NULL) {
|
if (wsound == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue