Issue #19636: Fix posix__getvolumepathname(), raise an OverflowError if

the length doesn't fit in an DWORD
This commit is contained in:
Victor Stinner 2013-11-24 19:22:57 +01:00
parent f4a4898c18
commit 6edddfaf5b
1 changed files with 14 additions and 5 deletions

View File

@ -385,6 +385,8 @@ static int win32_can_symlink = 0;
#endif #endif
#endif #endif
#define DWORD_MAX 4294967295U
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
static int static int
@ -4039,24 +4041,31 @@ posix__getvolumepathname(PyObject *self, PyObject *args)
{ {
PyObject *po, *result; PyObject *po, *result;
wchar_t *path, *mountpath=NULL; wchar_t *path, *mountpath=NULL;
size_t bufsize; size_t buflen;
BOOL ret; BOOL ret;
if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po))
return NULL; return NULL;
path = PyUnicode_AsUnicode(po); path = PyUnicode_AsUnicodeAndSize(po, &buflen);
if (path == NULL) if (path == NULL)
return NULL; return NULL;
buflen += 1;
/* Volume path should be shorter than entire path */ /* Volume path should be shorter than entire path */
bufsize = max(MAX_PATH, wcslen(path) * 2 * sizeof(wchar_t)+1); buflen = Py_MAX(buflen, MAX_PATH);
mountpath = (wchar_t *)PyMem_Malloc(bufsize);
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) if (mountpath == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = GetVolumePathNameW(path, mountpath, ret = GetVolumePathNameW(path, mountpath,
Py_SAFE_DOWNCAST(bufsize, size_t, DWORD)); Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (!ret) { if (!ret) {