Issue #23446: Use PyMem_New instead of PyMem_Malloc to avoid possible integer

overflows.  Added few missed PyErr_NoMemory().
This commit is contained in:
Serhiy Storchaka 2015-02-16 13:28:22 +02:00
parent e1efc07a30
commit 1a1ff29659
15 changed files with 50 additions and 52 deletions

View file

@ -1638,7 +1638,7 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
if(!buf_size)
return FALSE;
buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
buf = PyMem_New(wchar_t, buf_size+1);
if (!buf) {
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
@ -3627,7 +3627,7 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
len = wcslen(path->wide);
}
/* The +5 is so we can append "\\*.*\0" */
wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t));
wnamebuf = PyMem_New(wchar_t, len + 5);
if (!wnamebuf) {
PyErr_NoMemory();
goto exit;
@ -3917,7 +3917,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
Py_ARRAY_LENGTH(woutbuf),
woutbuf, &wtemp);
if (result > Py_ARRAY_LENGTH(woutbuf)) {
woutbufp = PyMem_Malloc(result * sizeof(wchar_t));
woutbufp = PyMem_New(wchar_t, result);
if (!woutbufp)
return PyErr_NoMemory();
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
@ -3997,7 +3997,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
if(!buf_size)
return win32_error_object("GetFinalPathNameByHandle", po);
target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
target_path = PyMem_New(wchar_t, buf_size+1);
if(!target_path)
return PyErr_NoMemory();
@ -4082,7 +4082,7 @@ posix__getvolumepathname(PyObject *self, PyObject *args)
return NULL;
}
mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t));
mountpath = PyMem_New(wchar_t, buflen);
if (mountpath == NULL)
return PyErr_NoMemory();
@ -6213,9 +6213,9 @@ posix_getgrouplist(PyObject *self, PyObject *args)
#endif
#ifdef __APPLE__
groups = PyMem_Malloc(ngroups * sizeof(int));
groups = PyMem_New(int, ngroups);
#else
groups = PyMem_Malloc(ngroups * sizeof(gid_t));
groups = PyMem_New(gid_t, ngroups);
#endif
if (groups == NULL)
return PyErr_NoMemory();
@ -6293,7 +6293,7 @@ posix_getgroups(PyObject *self, PyObject *noargs)
/* groups will fit in existing array */
alt_grouplist = grouplist;
} else {
alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
alt_grouplist = PyMem_New(gid_t, n);
if (alt_grouplist == NULL) {
errno = EINVAL;
return posix_error();
@ -6319,7 +6319,7 @@ posix_getgroups(PyObject *self, PyObject *noargs)
/* Avoid malloc(0) */
alt_grouplist = grouplist;
} else {
alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
alt_grouplist = PyMem_New(gid_t, n);
if (alt_grouplist == NULL) {
errno = EINVAL;
return posix_error();