diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -361,10 +361,17 @@ PyCurses_ConvertToString(PyCursesWindowO { if (PyUnicode_Check(obj)) { #ifdef HAVE_NCURSESW + Py_ssize_t wlen; assert (wstr != NULL); - *wstr = PyUnicode_AsWideCharString(obj, NULL); + + *wstr = PyUnicode_AsWideCharString(obj, &wlen); if (*wstr == NULL) return 0; + if (wlen != wcslen(wstr)) { + PyErr_SetString(PyExc_TypeError, + "embedded null character"); + return 0; + } return 2; #else assert (wstr == NULL); diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -205,16 +205,27 @@ PyLocale_strcoll(PyObject* self, PyObjec { PyObject *os1, *os2, *result = NULL; wchar_t *ws1 = NULL, *ws2 = NULL; + Py_ssize_t wlen; if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2)) return NULL; /* Convert the unicode strings to wchar[]. */ - ws1 = PyUnicode_AsWideCharString(os1, NULL); + ws1 = PyUnicode_AsWideCharString(os1, &wlen); if (ws1 == NULL) goto done; + if (wlen != wcslen(ws1)) { + PyErr_SetString(PyExc_TypeError, + "embedded null character"); + goto done; + } ws2 = PyUnicode_AsWideCharString(os2, NULL); if (ws2 == NULL) goto done; + if (wlen != wcslen(ws2)) { + PyErr_SetString(PyExc_TypeError, + "embedded null character"); + goto done; + } /* Collate the strings. */ result = PyLong_FromLong(wcscoll(ws1, ws2)); done: @@ -246,6 +257,11 @@ PyLocale_strxfrm(PyObject* self, PyObjec s = PyUnicode_AsWideCharString(str, &n1); if (s == NULL) goto exit; + if (n1 != wcslen(s)) { + PyErr_SetString(PyExc_TypeError, + "embedded null character"); + goto exit; + } /* assume no change in size, first */ n1 = n1 + 1; diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -4123,6 +4123,7 @@ NullImporter_init(NullImporter *self, Py PyObject *pathobj; DWORD rv; wchar_t *path; + Py_ssize_t len; if (!_PyArg_NoKeywords("NullImporter()", kwds)) return -1; @@ -4136,9 +4137,16 @@ NullImporter_init(NullImporter *self, Py return -1; } - path = PyUnicode_AsWideCharString(pathobj, NULL); + path = PyUnicode_AsWideCharString(pathobj, &len); if (path == NULL) return -1; + if (len != wcslen(path)) { + PyErr_SetString(PyExc_TypeError, + "embedded null character"); + PyMem_Free(path); + return -1; + } + /* see issue1293 and issue3677: * stat() on Windows doesn't recognise paths like * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.