diff options
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d0bd2d9..ae45fc3 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -385,6 +385,8 @@ static int win32_can_symlink = 0; #endif #endif +#define DWORD_MAX 4294967295U + #ifdef MS_WINDOWS static int @@ -897,7 +899,7 @@ path_converter(PyObject *o, void *p) { length = PyBytes_GET_SIZE(bytes); #ifdef MS_WINDOWS - if (length > MAX_PATH) { + if (length > MAX_PATH-1) { FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); Py_DECREF(bytes); return 0; @@ -1376,18 +1378,18 @@ posix_1str(const char *func_name, PyObject *args, char *format, static BOOL __stdcall win32_chdir(LPCSTR path) { - char new_path[MAX_PATH+1]; + char new_path[MAX_PATH]; int result; char env[4] = "=x:"; if(!SetCurrentDirectoryA(path)) return FALSE; - result = GetCurrentDirectoryA(MAX_PATH+1, new_path); + result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); if (!result) return FALSE; /* In the ANSI API, there should not be any paths longer - than MAX_PATH. */ - assert(result <= MAX_PATH+1); + than MAX_PATH-1 (not including the final null character). */ + assert(result < Py_ARRAY_LENGTH(new_path)); if (strncmp(new_path, "\\\\", 2) == 0 || strncmp(new_path, "//", 2) == 0) /* UNC path, nothing to do. */ @@ -1401,16 +1403,16 @@ win32_chdir(LPCSTR path) static BOOL __stdcall win32_wchdir(LPCWSTR path) { - wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; + wchar_t _new_path[MAX_PATH], *new_path = _new_path; int result; wchar_t env[4] = L"=x:"; if(!SetCurrentDirectoryW(path)) return FALSE; - result = GetCurrentDirectoryW(MAX_PATH+1, new_path); + result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); if (!result) return FALSE; - if (result > MAX_PATH+1) { + if (result > Py_ARRAY_LENGTH(new_path)) { new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); if (!new_path) { SetLastError(ERROR_OUTOFMEMORY); @@ -3396,11 +3398,11 @@ posix_getcwd(int use_bytes) PyObject *resobj; DWORD len; Py_BEGIN_ALLOW_THREADS - len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); + len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); /* If the buffer is large enough, len does not include the terminating \0. If the buffer is too small, len includes the space needed for the terminator. */ - if (len >= sizeof wbuf/ sizeof wbuf[0]) { + if (len >= Py_ARRAY_LENGTH(wbuf)) { wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); if (wbuf2) len = GetCurrentDirectoryW(len, wbuf2); @@ -3581,10 +3583,10 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list) HANDLE hFindFile = INVALID_HANDLE_VALUE; BOOL result; WIN32_FIND_DATA FileData; - char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ + char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ char *bufptr = namebuf; /* only claim to have space for MAX_PATH */ - Py_ssize_t len = sizeof(namebuf)-5; + Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; PyObject *po = NULL; wchar_t *wnamebuf = NULL; @@ -3873,14 +3875,14 @@ static PyObject * posix__getfullpathname(PyObject *self, PyObject *args) { const char *path; - char outbuf[MAX_PATH*2]; + char outbuf[MAX_PATH]; char *temp; PyObject *po; if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { wchar_t *wpath; - wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; + wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; wchar_t *wtemp; DWORD result; PyObject *v; @@ -4039,24 +4041,31 @@ posix__getvolumepathname(PyObject *self, PyObject *args) { PyObject *po, *result; wchar_t *path, *mountpath=NULL; - size_t bufsize; + size_t buflen; BOOL ret; if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) return NULL; - path = PyUnicode_AsUnicode(po); + path = PyUnicode_AsUnicodeAndSize(po, &buflen); if (path == NULL) return NULL; + buflen += 1; /* Volume path should be shorter than entire path */ - bufsize = max(MAX_PATH, wcslen(path) * 2 * sizeof(wchar_t)+1); - mountpath = (wchar_t *)PyMem_Malloc(bufsize); + buflen = Py_MAX(buflen, MAX_PATH); + + if (buflen > DWORD_MAX) { + PyErr_SetString(PyExc_OverflowError, "path too long"); + return NULL; + } + + mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); if (mountpath == NULL) return PyErr_NoMemory(); Py_BEGIN_ALLOW_THREADS ret = GetVolumePathNameW(path, mountpath, - Py_SAFE_DOWNCAST(bufsize, size_t, DWORD)); + Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); Py_END_ALLOW_THREADS if (!ret) { |