From bb6eb857cf055079b1108a2afd5a434433c97e90 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 17 Mar 2011 23:34:33 +0100 Subject: [PATCH 1/2] Issue #10914: fix bogus memory management in Modules/getpath.c, leading to a possible crash when calling Py_SetPath() --- Modules/getpath.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Modules/getpath.c b/Modules/getpath.c index 59623d79fcd..b7f9573f696 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -134,6 +134,7 @@ static wchar_t prefix[MAXPATHLEN+1]; static wchar_t exec_prefix[MAXPATHLEN+1]; static wchar_t progpath[MAXPATHLEN+1]; static wchar_t *module_search_path = NULL; +static int module_search_path_malloced = 0; static wchar_t *lib_python = L"lib/python" VERSION; static void @@ -634,7 +635,6 @@ calculate_path(void) bufsz += wcslen(zip_path) + 1; bufsz += wcslen(exec_prefix) + 1; - /* This is the only malloc call in this file */ buf = (wchar_t *)PyMem_Malloc(bufsz*sizeof(wchar_t)); if (buf == NULL) { @@ -687,6 +687,7 @@ calculate_path(void) /* And publish the results */ module_search_path = buf; + module_search_path_malloced = 1; } /* Reduce prefix and exec_prefix to their essence, @@ -726,15 +727,18 @@ void Py_SetPath(const wchar_t *path) { if (module_search_path != NULL) { - free(module_search_path); + if (module_search_path_malloced) + PyMem_Free(module_search_path); module_search_path = NULL; + module_search_path_malloced = 0; } if (path != NULL) { extern wchar_t *Py_GetProgramName(void); wchar_t *prog = Py_GetProgramName(); wcsncpy(progpath, prog, MAXPATHLEN); exec_prefix[0] = prefix[0] = L'\0'; - module_search_path = malloc((wcslen(path) + 1) * sizeof(wchar_t)); + module_search_path = PyMem_Malloc((wcslen(path) + 1) * sizeof(wchar_t)); + module_search_path_malloced = 1; if (module_search_path != NULL) wcscpy(module_search_path, path); } From 819be3406d2311347838a413f1893e4f20b3159c Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 17 Mar 2011 23:36:13 +0100 Subject: [PATCH 2/2] Add news entry for a791dd7d51f3 --- Misc/NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 328e44d01d3..5732a0de6e8 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.1? Core and Builtins ----------------- +- Issue #10914: fix bogus memory management in Modules/getpath.c, leading to + a possible crash when calling Py_SetPath(). + - Issue #11510: Fixed optimizer bug which turned "a,b={1,1}" into "a,b=(1,1)". - Issue #11432: A bug was introduced in subprocess.Popen on posix systems with