Kaydet (Commit) 72967a4c authored tarafından Victor Stinner's avatar Victor Stinner

calculate_path() now fails with a fatal error when it fails to allocate memory

for module_search_path. It was already the case on _Py_char2wchar() failure.
üst 2a1838b9
...@@ -134,7 +134,6 @@ static wchar_t prefix[MAXPATHLEN+1]; ...@@ -134,7 +134,6 @@ static wchar_t prefix[MAXPATHLEN+1];
static wchar_t exec_prefix[MAXPATHLEN+1]; static wchar_t exec_prefix[MAXPATHLEN+1];
static wchar_t progpath[MAXPATHLEN+1]; static wchar_t progpath[MAXPATHLEN+1];
static wchar_t *module_search_path = NULL; static wchar_t *module_search_path = NULL;
static int module_search_path_malloced = 0;
static void static void
reduce(wchar_t *dir) reduce(wchar_t *dir)
...@@ -740,60 +739,55 @@ calculate_path(void) ...@@ -740,60 +739,55 @@ calculate_path(void)
bufsz += wcslen(zip_path) + 1; bufsz += wcslen(zip_path) + 1;
bufsz += wcslen(exec_prefix) + 1; bufsz += wcslen(exec_prefix) + 1;
buf = (wchar_t *)PyMem_Malloc(bufsz*sizeof(wchar_t)); buf = (wchar_t *)PyMem_Malloc(bufsz * sizeof(wchar_t));
if (buf == NULL) { if (buf == NULL) {
/* We can't exit, so print a warning and limp along */ Py_FatalError(
fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); "Not enough memory for dynamic PYTHONPATH");
fprintf(stderr, "Using default static PYTHONPATH.\n");
module_search_path = L"" PYTHONPATH;
} }
else {
/* Run-time value of $PYTHONPATH goes first */
if (rtpypath) {
wcscpy(buf, rtpypath);
wcscat(buf, delimiter);
}
else
buf[0] = '\0';
/* Next is the default zip path */ /* Run-time value of $PYTHONPATH goes first */
wcscat(buf, zip_path); if (rtpypath) {
wcscpy(buf, rtpypath);
wcscat(buf, delimiter); wcscat(buf, delimiter);
}
else
buf[0] = '\0';
/* Next goes merge of compile-time $PYTHONPATH with /* Next is the default zip path */
* dynamically located prefix. wcscat(buf, zip_path);
*/ wcscat(buf, delimiter);
defpath = _pythonpath;
while (1) {
wchar_t *delim = wcschr(defpath, DELIM);
if (defpath[0] != SEP) { /* Next goes merge of compile-time $PYTHONPATH with
wcscat(buf, prefix); * dynamically located prefix.
wcscat(buf, separator); */
} defpath = _pythonpath;
while (1) {
wchar_t *delim = wcschr(defpath, DELIM);
if (delim) { if (defpath[0] != SEP) {
size_t len = delim - defpath + 1; wcscat(buf, prefix);
size_t end = wcslen(buf) + len; wcscat(buf, separator);
wcsncat(buf, defpath, len);
*(buf + end) = '\0';
}
else {
wcscat(buf, defpath);
break;
}
defpath = delim + 1;
} }
wcscat(buf, delimiter);
/* Finally, on goes the directory for dynamic-load modules */
wcscat(buf, exec_prefix);
/* And publish the results */ if (delim) {
module_search_path = buf; size_t len = delim - defpath + 1;
module_search_path_malloced = 1; size_t end = wcslen(buf) + len;
wcsncat(buf, defpath, len);
*(buf + end) = '\0';
}
else {
wcscat(buf, defpath);
break;
}
defpath = delim + 1;
} }
wcscat(buf, delimiter);
/* Finally, on goes the directory for dynamic-load modules */
wcscat(buf, exec_prefix);
/* And publish the results */
module_search_path = buf;
/* Reduce prefix and exec_prefix to their essence, /* Reduce prefix and exec_prefix to their essence,
* e.g. /usr/local/lib/python1.5 is reduced to /usr/local. * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
...@@ -834,10 +828,8 @@ void ...@@ -834,10 +828,8 @@ void
Py_SetPath(const wchar_t *path) Py_SetPath(const wchar_t *path)
{ {
if (module_search_path != NULL) { if (module_search_path != NULL) {
if (module_search_path_malloced) PyMem_RawFree(module_search_path);
PyMem_RawFree(module_search_path);
module_search_path = NULL; module_search_path = NULL;
module_search_path_malloced = 0;
} }
if (path != NULL) { if (path != NULL) {
extern wchar_t *Py_GetProgramName(void); extern wchar_t *Py_GetProgramName(void);
...@@ -845,7 +837,6 @@ Py_SetPath(const wchar_t *path) ...@@ -845,7 +837,6 @@ Py_SetPath(const wchar_t *path)
wcsncpy(progpath, prog, MAXPATHLEN); wcsncpy(progpath, prog, MAXPATHLEN);
exec_prefix[0] = prefix[0] = L'\0'; exec_prefix[0] = prefix[0] = L'\0';
module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t)); module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
module_search_path_malloced = 1;
if (module_search_path != NULL) if (module_search_path != NULL)
wcscpy(module_search_path, path); wcscpy(module_search_path, path);
} }
......
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