Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization

* Replace malloc() with PyMem_RawMalloc()
* Replace PyMem_Malloc() with PyMem_RawMalloc() where the GIL is not held.
* _Py_char2wchar() now returns a buffer allocated by PyMem_RawMalloc(), instead
  of PyMem_Malloc()
This commit is contained in:
Victor Stinner 2013-07-07 16:25:15 +02:00
parent 51fa458d0a
commit 1a7425f67a
8 changed files with 76 additions and 76 deletions

View file

@ -245,9 +245,9 @@ getpythonregpath(HKEY keyBase, int skipcore)
/* Tried to use sysget("winver") but here is too early :-( */
versionLen = strlen(PyWin_DLLVersionString);
/* Space for all the chars, plus one \0 */
keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
sizeof(WCHAR)*(versionLen-1) +
sizeof(keySuffix));
keyBuf = keyBufPtr = PyMem_RawMalloc(sizeof(keyPrefix) +
sizeof(WCHAR)*(versionLen-1) +
sizeof(keySuffix));
if (keyBuf==NULL) goto done;
memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
@ -271,7 +271,7 @@ getpythonregpath(HKEY keyBase, int skipcore)
/* Allocate a temp array of char buffers, so we only need to loop
reading the registry once
*/
ppPaths = malloc( sizeof(WCHAR *) * numKeys );
ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
if (ppPaths==NULL) goto done;
memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
/* Loop over all subkeys, allocating a temp sub-buffer. */
@ -293,7 +293,7 @@ getpythonregpath(HKEY keyBase, int skipcore)
/* Find the value of the buffer size, malloc, then read it */
RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
if (reqdSize) {
ppPaths[index] = malloc(reqdSize);
ppPaths[index] = PyMem_RawMalloc(reqdSize);
if (ppPaths[index]) {
RegQueryValueExW(subKey, NULL, 0, NULL,
(LPBYTE)ppPaths[index],
@ -308,7 +308,7 @@ getpythonregpath(HKEY keyBase, int skipcore)
if (dataSize == 0) goto done;
/* original datasize from RegQueryInfo doesn't include the \0 */
dataBuf = malloc((dataSize+1) * sizeof(WCHAR));
dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
if (dataBuf) {
WCHAR *szCur = dataBuf;
DWORD reqdSize = dataSize;
@ -346,14 +346,13 @@ getpythonregpath(HKEY keyBase, int skipcore)
done:
/* Loop freeing my temp buffers */
if (ppPaths) {
for(index=0;index<numKeys;index++)
if (ppPaths[index]) free(ppPaths[index]);
free(ppPaths);
for(index=0; index<numKeys; index++)
PyMem_RawFree(ppPaths[index]);
PyMem_RawFree(ppPaths);
}
if (newKey)
RegCloseKey(newKey);
if (keyBuf)
free(keyBuf);
PyMem_RawFree(keyBuf);
return retval;
}
#endif /* Py_ENABLE_SHARED */
@ -616,7 +615,7 @@ calculate_path(void)
if (envpath != NULL)
bufsz += wcslen(envpath) + 1;
module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
if (buf == NULL) {
/* We can't exit, so print a warning and limp along */
fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
@ -629,10 +628,8 @@ calculate_path(void)
module_search_path = PYTHONPATH;
}
#ifdef MS_WINDOWS
if (machinepath)
free(machinepath);
if (userpath)
free(userpath);
PyMem_RawFree(machinepath);
PyMem_RawFree(userpath);
#endif /* MS_WINDOWS */
return;
}
@ -652,13 +649,13 @@ calculate_path(void)
wcscpy(buf, userpath);
buf = wcschr(buf, L'\0');
*buf++ = DELIM;
free(userpath);
PyMem_RawFree(userpath);
}
if (machinepath) {
wcscpy(buf, machinepath);
buf = wcschr(buf, L'\0');
*buf++ = DELIM;
free(machinepath);
PyMem_RawFree(machinepath);
}
if (pythonhome == NULL) {
if (!skipdefault) {
@ -745,7 +742,7 @@ void
Py_SetPath(const wchar_t *path)
{
if (module_search_path != NULL) {
free(module_search_path);
PyMem_RawFree(module_search_path);
module_search_path = NULL;
}
if (path != NULL) {
@ -753,10 +750,10 @@ Py_SetPath(const wchar_t *path)
wchar_t *prog = Py_GetProgramName();
wcsncpy(progpath, prog, MAXPATHLEN);
prefix[0] = L'\0';
module_search_path = malloc((wcslen(path) + 1) * sizeof(wchar_t));
module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
if (module_search_path != NULL)
wcscpy(module_search_path, path);
}
}
}
wchar_t *