Commit 7377daff authored by Victor Stinner's avatar Victor Stinner

Issue #19636: Fix usage of MAX_PATH in posixmodule.c

parent af2b278b
...@@ -899,7 +899,7 @@ path_converter(PyObject *o, void *p) { ...@@ -899,7 +899,7 @@ path_converter(PyObject *o, void *p) {
length = PyBytes_GET_SIZE(bytes); length = PyBytes_GET_SIZE(bytes);
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
if (length > MAX_PATH) { if (length > MAX_PATH-1) {
FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Py_DECREF(bytes); Py_DECREF(bytes);
return 0; return 0;
...@@ -1378,18 +1378,18 @@ posix_1str(const char *func_name, PyObject *args, char *format, ...@@ -1378,18 +1378,18 @@ posix_1str(const char *func_name, PyObject *args, char *format,
static BOOL __stdcall static BOOL __stdcall
win32_chdir(LPCSTR path) win32_chdir(LPCSTR path)
{ {
char new_path[MAX_PATH+1]; char new_path[MAX_PATH];
int result; int result;
char env[4] = "=x:"; char env[4] = "=x:";
if(!SetCurrentDirectoryA(path)) if(!SetCurrentDirectoryA(path))
return FALSE; return FALSE;
result = GetCurrentDirectoryA(MAX_PATH+1, new_path); result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path);
if (!result) if (!result)
return FALSE; return FALSE;
/* In the ANSI API, there should not be any paths longer /* In the ANSI API, there should not be any paths longer
than MAX_PATH. */ than MAX_PATH-1 (not including the final null character). */
assert(result <= MAX_PATH+1); assert(result < Py_ARRAY_LENGTH(new_path));
if (strncmp(new_path, "\\\\", 2) == 0 || if (strncmp(new_path, "\\\\", 2) == 0 ||
strncmp(new_path, "//", 2) == 0) strncmp(new_path, "//", 2) == 0)
/* UNC path, nothing to do. */ /* UNC path, nothing to do. */
...@@ -1403,16 +1403,16 @@ win32_chdir(LPCSTR path) ...@@ -1403,16 +1403,16 @@ win32_chdir(LPCSTR path)
static BOOL __stdcall static BOOL __stdcall
win32_wchdir(LPCWSTR path) 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; int result;
wchar_t env[4] = L"=x:"; wchar_t env[4] = L"=x:";
if(!SetCurrentDirectoryW(path)) if(!SetCurrentDirectoryW(path))
return FALSE; return FALSE;
result = GetCurrentDirectoryW(MAX_PATH+1, new_path); result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path);
if (!result) if (!result)
return FALSE; return FALSE;
if (result > MAX_PATH+1) { if (result > Py_ARRAY_LENGTH(new_path)) {
new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
if (!new_path) { if (!new_path) {
SetLastError(ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
...@@ -3398,11 +3398,11 @@ posix_getcwd(int use_bytes) ...@@ -3398,11 +3398,11 @@ posix_getcwd(int use_bytes)
PyObject *resobj; PyObject *resobj;
DWORD len; DWORD len;
Py_BEGIN_ALLOW_THREADS 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 /* If the buffer is large enough, len does not include the
terminating \0. If the buffer is too small, len includes terminating \0. If the buffer is too small, len includes
the space needed for the terminator. */ 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)); wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
if (wbuf2) if (wbuf2)
len = GetCurrentDirectoryW(len, wbuf2); len = GetCurrentDirectoryW(len, wbuf2);
...@@ -3583,10 +3583,10 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list) ...@@ -3583,10 +3583,10 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
HANDLE hFindFile = INVALID_HANDLE_VALUE; HANDLE hFindFile = INVALID_HANDLE_VALUE;
BOOL result; BOOL result;
WIN32_FIND_DATA FileData; WIN32_FIND_DATA FileData;
char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
char *bufptr = namebuf; char *bufptr = namebuf;
/* only claim to have space for MAX_PATH */ /* 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; PyObject *po = NULL;
wchar_t *wnamebuf = NULL; wchar_t *wnamebuf = NULL;
...@@ -3875,14 +3875,14 @@ static PyObject * ...@@ -3875,14 +3875,14 @@ static PyObject *
posix__getfullpathname(PyObject *self, PyObject *args) posix__getfullpathname(PyObject *self, PyObject *args)
{ {
const char *path; const char *path;
char outbuf[MAX_PATH*2]; char outbuf[MAX_PATH];
char *temp; char *temp;
PyObject *po; PyObject *po;
if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
{ {
wchar_t *wpath; wchar_t *wpath;
wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
wchar_t *wtemp; wchar_t *wtemp;
DWORD result; DWORD result;
PyObject *v; PyObject *v;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment