diff -r 6ce4868861ba Python/import.c --- a/Python/import.c Thu Mar 15 13:24:40 2012 -0700 +++ b/Python/import.c Thu Mar 15 23:04:42 2012 -0700 @@ -996,7 +996,7 @@ { struct stat st; FILE *fpc; - char buf[MAXPATHLEN+1]; + char *buf = NULL; char *cpathname; PyCodeObject *co; PyObject *m; @@ -1015,6 +1015,10 @@ */ st.st_mtime &= 0xFFFFFFFF; } + if (!(buf = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } cpathname = make_compiled_pathname(pathname, buf, (size_t)MAXPATHLEN + 1); if (cpathname != NULL && @@ -1022,9 +1026,9 @@ co = read_compiled_module(cpathname, fpc); fclose(fpc); if (co == NULL) - return NULL; + goto error_exit; if (update_compiled_module(co, pathname) < 0) - return NULL; + goto error_exit; if (Py_VerboseFlag) PySys_WriteStderr("import %s # precompiled from %s\n", name, cpathname); @@ -1033,7 +1037,7 @@ else { co = parse_source_module(pathname, fp); if (co == NULL) - return NULL; + goto error_exit; if (Py_VerboseFlag) PySys_WriteStderr("import %s # from %s\n", name, pathname); @@ -1046,7 +1050,12 @@ m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); Py_DECREF(co); + free(buf); return m; + +error_exit: + if (buf) free(buf); + return NULL; } @@ -1066,7 +1075,7 @@ PyObject *file = NULL; PyObject *path = NULL; int err; - char buf[MAXPATHLEN+1]; + char *buf = NULL; FILE *fp = NULL; struct filedescr *fdp; @@ -1088,8 +1097,12 @@ err = PyDict_SetItemString(d, "__path__", path); if (err != 0) goto error; + if (!(buf = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + goto error; + } buf[0] = '\0'; - fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL); + fdp = find_module(name, "__init__", path, buf, MAXPATHLEN+1, &fp, NULL); if (fdp == NULL) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { PyErr_Clear(); @@ -1107,6 +1120,7 @@ error: m = NULL; cleanup: + if (buf) free(buf); Py_XDECREF(path); Py_XDECREF(file); return m; @@ -1235,7 +1249,7 @@ static struct filedescr fd_frozen = {"", "", PY_FROZEN}; static struct filedescr fd_builtin = {"", "", C_BUILTIN}; static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; - char name[MAXPATHLEN+1]; + char *name = NULL; #if defined(PYOS_OS2) size_t saved_len; size_t saved_namelen; @@ -1249,6 +1263,10 @@ "module name is too long"); return NULL; } + if (!(name = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } strcpy(name, subname); /* sys.meta_path import hook */ @@ -1260,7 +1278,7 @@ PyErr_SetString(PyExc_RuntimeError, "sys.meta_path must be a list of " "import hooks"); - return NULL; + goto error_exit; } Py_INCREF(meta_path); /* zap guard */ npath = PyList_Size(meta_path); @@ -1273,12 +1291,13 @@ path : Py_None); if (loader == NULL) { Py_DECREF(meta_path); - return NULL; /* true error */ + goto error_exit; /* true error */ } if (loader != Py_None) { /* a loader was found */ *p_loader = loader; Py_DECREF(meta_path); + free(name); return &importhookdescr; } Py_DECREF(loader); @@ -1292,7 +1311,7 @@ if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) { PyErr_SetString(PyExc_ImportError, "full frozen module name too long"); - return NULL; + goto error_exit; } strcpy(buf, PyString_AsString(path)); strcat(buf, "."); @@ -1300,19 +1319,22 @@ strcpy(name, buf); if (find_frozen(name) != NULL) { strcpy(buf, name); + free(name); return &fd_frozen; } PyErr_Format(PyExc_ImportError, "No frozen submodule named %.200s", name); - return NULL; + goto error_exit; } if (path == NULL) { if (is_builtin(name)) { strcpy(buf, name); + free(name); return &fd_builtin; } if ((find_frozen(name)) != NULL) { strcpy(buf, name); + free(name); return &fd_frozen; } @@ -1320,6 +1342,7 @@ fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); if (fp != NULL) { *p_fp = fp; + free(name); return fdp; } #endif @@ -1328,7 +1351,7 @@ if (path == NULL || !PyList_Check(path)) { PyErr_SetString(PyExc_RuntimeError, "sys.path must be a list of directory names"); - return NULL; + goto error_exit; } path_hooks = PySys_GetObject("path_hooks"); @@ -1336,14 +1359,14 @@ PyErr_SetString(PyExc_RuntimeError, "sys.path_hooks must be a list of " "import hooks"); - return NULL; + goto error_exit; } path_importer_cache = PySys_GetObject("path_importer_cache"); if (path_importer_cache == NULL || !PyDict_Check(path_importer_cache)) { PyErr_SetString(PyExc_RuntimeError, "sys.path_importer_cache must be a dict"); - return NULL; + goto error_exit; } npath = PyList_Size(path); @@ -1352,13 +1375,13 @@ PyObject *copy = NULL; PyObject *v = PyList_GetItem(path, i); if (!v) - return NULL; + goto error_exit; #ifdef Py_USING_UNICODE if (PyUnicode_Check(v)) { copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v), PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL); if (copy == NULL) - return NULL; + goto error_exit; v = copy; } else @@ -1384,7 +1407,7 @@ path_hooks, v); if (importer == NULL) { Py_XDECREF(copy); - return NULL; + goto error_exit; } /* Note: importer is a borrowed reference */ if (importer != Py_None) { @@ -1394,10 +1417,11 @@ "s", fullname); Py_XDECREF(copy); if (loader == NULL) - return NULL; /* error */ + goto error_exit; /* error */ if (loader != Py_None) { /* a loader was found */ *p_loader = loader; + free(name); return &importhookdescr; } Py_DECREF(loader); @@ -1421,6 +1445,7 @@ case_ok(buf, len, namelen, name)) { /* case matches */ if (find_init_module(buf)) { /* and has __init__.py */ Py_XDECREF(copy); + free(name); return &fd_package; } else { @@ -1431,7 +1456,7 @@ if (PyErr_Warn(PyExc_ImportWarning, warnstr)) { Py_XDECREF(copy); - return NULL; + goto error_exit; } } } @@ -1506,10 +1531,15 @@ if (fp == NULL) { PyErr_Format(PyExc_ImportError, "No module named %.200s", name); - return NULL; + goto error_exit; } *p_fp = fp; + free(name); return fdp; + +error_exit: + if (name) free(name); + return NULL; } /* Helpers for main.c @@ -2116,7 +2146,7 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level) { - char buf[MAXPATHLEN+1]; + char *buf = NULL; Py_ssize_t buflen = 0; PyObject *parent, *head, *next, *tail; @@ -2130,14 +2160,18 @@ return NULL; } + if (!(buf = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } parent = get_parent(globals, buf, &buflen, level); if (parent == NULL) - return NULL; + goto error_exit; head = load_next(parent, level < 0 ? Py_None : parent, &name, buf, &buflen); if (head == NULL) - return NULL; + goto error_exit; tail = head; Py_INCREF(tail); @@ -2146,7 +2180,7 @@ Py_DECREF(tail); if (next == NULL) { Py_DECREF(head); - return NULL; + goto error_exit; } tail = next; } @@ -2158,7 +2192,7 @@ Py_DECREF(head); PyErr_SetString(PyExc_ValueError, "Empty module name"); - return NULL; + goto error_exit; } if (fromlist != NULL) { @@ -2168,16 +2202,22 @@ if (fromlist == NULL) { Py_DECREF(tail); + free(buf); return head; } Py_DECREF(head); if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { Py_DECREF(tail); - return NULL; + goto error_exit; } + free(buf); return tail; + +error_exit: + if (buf) free(buf); + return NULL; } PyObject * @@ -2567,7 +2607,7 @@ } else { PyObject *path, *loader = NULL; - char buf[MAXPATHLEN+1]; + char *buf = NULL; struct filedescr *fdp; FILE *fp = NULL; @@ -2582,11 +2622,16 @@ } } + if (!(buf = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } buf[0] = '\0'; fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1, &fp, &loader); Py_XDECREF(path); if (fdp == NULL) { + free(buf); if (!PyErr_ExceptionMatches(PyExc_ImportError)) return NULL; PyErr_Clear(); @@ -2601,6 +2646,7 @@ Py_XDECREF(m); m = NULL; } + free(buf); } return m; @@ -2618,7 +2664,7 @@ PyObject *modules = PyImport_GetModuleDict(); PyObject *path = NULL, *loader = NULL, *existing_m = NULL; char *name, *subname; - char buf[MAXPATHLEN+1]; + char *buf = NULL; struct filedescr *fdp; FILE *fp = NULL; PyObject *newm; @@ -2678,6 +2724,11 @@ if (path == NULL) PyErr_Clear(); } + if (!(buf = malloc(MAXPATHLEN+1))) { + Py_XDECREF(path); + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } buf[0] = '\0'; fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader); Py_XDECREF(path); @@ -2685,6 +2736,7 @@ if (fdp == NULL) { Py_XDECREF(loader); imp_modules_reloading_clear(); + free(buf); return NULL; } @@ -2702,6 +2754,7 @@ PyDict_SetItemString(modules, name, m); } imp_modules_reloading_clear(); + free(buf); return newm; } @@ -2832,19 +2885,27 @@ extern int fclose(FILE *); PyObject *fob, *ret; struct filedescr *fdp; - char pathname[MAXPATHLEN+1]; + char *pathname = NULL; FILE *fp = NULL; + if (!(pathname = malloc(MAXPATHLEN+1))) { + PyErr_SetString(PyExc_MemoryError, "malloc failed"); + return NULL; + } pathname[0] = '\0'; if (path == Py_None) path = NULL; fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL); - if (fdp == NULL) + if (fdp == NULL) { + free(pathname); return NULL; + } if (fp != NULL) { fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); - if (fob == NULL) + if (fob == NULL) { + free(pathname); return NULL; + } } else { fob = Py_None; @@ -2853,6 +2914,7 @@ ret = Py_BuildValue("Os(ssi)", fob, pathname, fdp->suffix, fdp->mode, fdp->type); Py_DECREF(fob); + free(pathname); return ret; }