diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-28 07:31:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-28 07:31:00 (GMT) |
commit | 54ba940abc2fabb94fede46dfad80f8ac15632a3 (patch) | |
tree | 6c5d594ac7e15a9b50a04a8f674e2362602ab9de /Python | |
parent | 9dff523e42a5f8eca8e88151785be54468e57969 (diff) | |
download | cpython-54ba940abc2fabb94fede46dfad80f8ac15632a3.zip cpython-54ba940abc2fabb94fede46dfad80f8ac15632a3.tar.gz cpython-54ba940abc2fabb94fede46dfad80f8ac15632a3.tar.bz2 |
[3.5] bpo-13617: Reject embedded null characters in wchar* strings. (GH-2302) (#2463)
Based on patch by Victor Stinner.
Add private C API function _PyUnicode_AsUnicode() which is similar to
PyUnicode_AsUnicode(), but checks for null characters..
(cherry picked from commit f7eae0adfcd4c50034281b2c69f461b43b68db84)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/dynload_win.c | 4 | ||||
-rw-r--r-- | Python/fileutils.c | 23 |
2 files changed, 19 insertions, 8 deletions
diff --git a/Python/dynload_win.c b/Python/dynload_win.c index f2c796e..3f533d1 100644 --- a/Python/dynload_win.c +++ b/Python/dynload_win.c @@ -192,13 +192,13 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, { dl_funcptr p; char funcname[258], *import_python; - wchar_t *wpathname; + const wchar_t *wpathname; #ifndef _DEBUG _Py_CheckPython3(); #endif - wpathname = PyUnicode_AsUnicode(pathname); + wpathname = _PyUnicode_AsUnicode(pathname); if (wpathname == NULL) return NULL; diff --git a/Python/fileutils.c b/Python/fileutils.c index 23eed71..5072118 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -710,21 +710,32 @@ _Py_stat(PyObject *path, struct stat *statbuf) #ifdef MS_WINDOWS int err; struct _stat wstatbuf; - wchar_t *wpath; + const wchar_t *wpath; - wpath = PyUnicode_AsUnicode(path); + wpath = _PyUnicode_AsUnicode(path); if (wpath == NULL) return -2; + err = _wstat(wpath, &wstatbuf); if (!err) statbuf->st_mode = wstatbuf.st_mode; return err; #else int ret; - PyObject *bytes = PyUnicode_EncodeFSDefault(path); + PyObject *bytes; + char *cpath; + + bytes = PyUnicode_EncodeFSDefault(path); if (bytes == NULL) return -2; - ret = stat(PyBytes_AS_STRING(bytes), statbuf); + + /* check for embedded null bytes */ + if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) { + Py_DECREF(bytes); + return -2; + } + + ret = stat(cpath, statbuf); Py_DECREF(bytes); return ret; #endif @@ -1083,7 +1094,7 @@ _Py_fopen_obj(PyObject *path, const char *mode) FILE *f; int async_err = 0; #ifdef MS_WINDOWS - wchar_t *wpath; + const wchar_t *wpath; wchar_t wmode[10]; int usize; @@ -1097,7 +1108,7 @@ _Py_fopen_obj(PyObject *path, const char *mode) Py_TYPE(path)); return NULL; } - wpath = PyUnicode_AsUnicode(path); + wpath = _PyUnicode_AsUnicode(path); if (wpath == NULL) return NULL; |