Index: Python/import.c =================================================================== --- Python/import.c (révision 80303) +++ Python/import.c (copie de travail) @@ -968,7 +968,7 @@ { struct stat st; FILE *fpc; - char buf[MAXPATHLEN+1]; + char *buf; char *cpathname; PyCodeObject *co; PyObject *m; @@ -990,6 +990,13 @@ return NULL; } #endif + + buf = (char*)malloc(MAXPATHLEN+1); + if (buf == NULL) { + PyErr_NoMemory(); + return NULL; + } + cpathname = make_compiled_pathname(pathname, buf, (size_t)MAXPATHLEN + 1); if (cpathname != NULL && @@ -997,9 +1004,9 @@ co = read_compiled_module(cpathname, fpc); fclose(fpc); if (co == NULL) - return NULL; + goto error; if (update_compiled_module(co, pathname) < 0) - return NULL; + goto error; if (Py_VerboseFlag) PySys_WriteStderr("import %s # precompiled from %s\n", name, cpathname); @@ -1008,7 +1015,7 @@ else { co = parse_source_module(pathname, fp); if (co == NULL) - return NULL; + goto error; if (Py_VerboseFlag) PySys_WriteStderr("import %s # from %s\n", name, pathname); @@ -1020,8 +1027,13 @@ } m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); Py_DECREF(co); + free(buf); return m; + +error: + free(buf); + return NULL; } @@ -1029,7 +1041,7 @@ static PyObject *load_module(char *, FILE *, char *, int, PyObject *); static struct filedescr *find_module(char *, char *, PyObject *, char *, size_t, FILE **, PyObject **); -static struct _frozen *find_frozen(char *name); +static struct _frozen *find_frozen(const char *name); /* Load a package and return its module object WITH INCREMENTED REFERENCE COUNT */ @@ -1041,7 +1053,7 @@ PyObject *file = NULL; PyObject *path = NULL; int err; - char buf[MAXPATHLEN+1]; + char *buf = NULL; FILE *fp = NULL; struct filedescr *fdp; @@ -1063,8 +1075,14 @@ err = PyDict_SetItemString(d, "__path__", path); if (err != 0) goto error; + + buf = (char*)malloc(MAXPATHLEN+1); + if (buf == NULL) { + PyErr_NoMemory(); + 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(); @@ -1082,6 +1100,8 @@ error: m = NULL; cleanup: + if (buf) + free(buf); Py_XDECREF(path); Py_XDECREF(file); return m; @@ -1213,7 +1233,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; #if defined(PYOS_OS2) size_t saved_len; size_t saved_namelen; @@ -1227,7 +1247,11 @@ "module name is too long"); return NULL; } - strcpy(name, subname); + name = strdup(subname); + if (name == NULL) { + PyErr_NoMemory(); + return NULL; + } /* sys.meta_path import hook */ if (p_loader != NULL) { @@ -1238,7 +1262,7 @@ PyErr_SetString(PyExc_ImportError, "sys.meta_path must be a list of " "import hooks"); - return NULL; + goto error; } Py_INCREF(meta_path); /* zap guard */ npath = PyList_Size(meta_path); @@ -1251,13 +1275,14 @@ path : Py_None); if (loader == NULL) { Py_DECREF(meta_path); - return NULL; /* true error */ + goto error; /* true error */ } if (loader != Py_None) { /* a loader was found */ *p_loader = loader; Py_DECREF(meta_path); - return &importhookdescr; + fdp = &importhookdescr; + goto finally; } Py_DECREF(loader); } @@ -1270,35 +1295,38 @@ if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) { PyErr_SetString(PyExc_ImportError, "full frozen module name too long"); - return NULL; + goto error; } strcpy(buf, PyString_AsString(path)); strcat(buf, "."); strcat(buf, name); - strcpy(name, buf); + free(name); + name = strdup(buf); if (find_frozen(name) != NULL) { - strcpy(buf, name); - return &fd_frozen; + fdp = &fd_frozen; + goto finally; } PyErr_Format(PyExc_ImportError, "No frozen submodule named %.200s", name); - return NULL; + goto error; } if (path == NULL) { if (is_builtin(name)) { strcpy(buf, name); - return &fd_builtin; + fdp = &fd_builtin; + goto finally; } if ((find_frozen(name)) != NULL) { strcpy(buf, name); - return &fd_frozen; + fdp = &fd_frozen; + goto finally; } #ifdef MS_COREDLL fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); if (fp != NULL) { *p_fp = fp; - return fdp; + goto finally; } #endif path = PySys_GetObject("path"); @@ -1306,7 +1334,7 @@ if (path == NULL || !PyList_Check(path)) { PyErr_SetString(PyExc_ImportError, "sys.path must be a list of directory names"); - return NULL; + goto error; } path_hooks = PySys_GetObject("path_hooks"); @@ -1314,14 +1342,14 @@ PyErr_SetString(PyExc_ImportError, "sys.path_hooks must be a list of " "import hooks"); - return NULL; + goto error; } path_importer_cache = PySys_GetObject("path_importer_cache"); if (path_importer_cache == NULL || !PyDict_Check(path_importer_cache)) { PyErr_SetString(PyExc_ImportError, "sys.path_importer_cache must be a dict"); - return NULL; + goto error; } npath = PyList_Size(path); @@ -1330,13 +1358,13 @@ PyObject *copy = NULL; PyObject *v = PyList_GetItem(path, i); if (!v) - return NULL; + goto error; #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; v = copy; } else @@ -1362,7 +1390,7 @@ path_hooks, v); if (importer == NULL) { Py_XDECREF(copy); - return NULL; + goto error; } /* Note: importer is a borrowed reference */ if (importer != Py_None) { @@ -1372,11 +1400,12 @@ "s", fullname); Py_XDECREF(copy); if (loader == NULL) - return NULL; /* error */ + goto error; if (loader != Py_None) { /* a loader was found */ *p_loader = loader; - return &importhookdescr; + fdp = &importhookdescr; + goto finally; } Py_DECREF(loader); continue; @@ -1401,18 +1430,21 @@ case_ok(buf, len, namelen, name)) { /* case matches */ if (find_init_module(buf)) { /* and has __init__.py */ Py_XDECREF(copy); - return &fd_package; + fdp = &fd_package; + goto finally; } else { - char warnstr[MAXPATHLEN+80]; - sprintf(warnstr, "Not importing directory " + char *warnstr = NULL; + asprintf(&warnstr, "Not importing directory " "'%.*s': missing __init__.py", MAXPATHLEN, buf); if (PyErr_Warn(PyExc_ImportWarning, warnstr)) { + free(warnstr); Py_XDECREF(copy); - return NULL; + goto error; } + free(warnstr); } } #else @@ -1422,18 +1454,21 @@ case_ok(buf, len, namelen, name)) { if (find_init_module(buf)) { Py_XDECREF(copy); - return &fd_package; + fdp = &fd_package; + goto finally; } else { - char warnstr[MAXPATHLEN+80]; - sprintf(warnstr, "Not importing directory " + char *warnstr = NULL; + asprintf(&warnstr, "Not importing directory " "'%.*s': missing __init__.py", MAXPATHLEN, buf); if (PyErr_Warn(PyExc_ImportWarning, warnstr)) { + free(warnstr); Py_XDECREF(copy); - return NULL; + goto error; } + free(warnstr); } #endif #endif @@ -1508,9 +1543,17 @@ if (fp == NULL) { PyErr_Format(PyExc_ImportError, "No module named %.200s", name); - return NULL; + goto error; } *p_fp = fp; + + goto finally; + +error: + fdp = NULL; + +finally: + free(name); return fdp; } @@ -1930,7 +1973,7 @@ /* Frozen modules */ static struct _frozen * -find_frozen(char *name) +find_frozen(const char *name) { struct _frozen *p; @@ -2116,9 +2159,9 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level) { - char buf[MAXPATHLEN+1]; + char *buf; Py_ssize_t buflen = 0; - PyObject *parent, *head, *next, *tail; + PyObject *parent, *head, *next, *tail = NULL, *result; if (strchr(name, '/') != NULL #ifdef MS_WINDOWS @@ -2130,34 +2173,39 @@ return NULL; } + buf = (char*)malloc(MAXPATHLEN+1); + if (buf == NULL) { + PyErr_NoMemory(); + return NULL; + } + parent = get_parent(globals, buf, &buflen, level); if (parent == NULL) - return NULL; + goto error; head = load_next(parent, Py_None, &name, buf, &buflen); if (head == NULL) - return NULL; + goto error; tail = head; Py_INCREF(tail); while (name) { next = load_next(tail, tail, &name, buf, &buflen); - Py_DECREF(tail); if (next == NULL) { Py_DECREF(head); - return NULL; + goto error; } + Py_DECREF(tail); tail = next; } if (tail == Py_None) { /* If tail is Py_None, both get_parent and load_next found an empty module name: someone called __import__("") or doctored faulty bytecode */ - Py_DECREF(tail); Py_DECREF(head); PyErr_SetString(PyExc_ValueError, "Empty module name"); - return NULL; + goto error; } if (fromlist != NULL) { @@ -2166,17 +2214,24 @@ } if (fromlist == NULL) { - Py_DECREF(tail); - return head; + result = head; + goto finally; } Py_DECREF(head); - if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { - Py_DECREF(tail); - return NULL; - } + if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) + goto error; - return tail; + result = tail; + tail = NULL; + goto finally; + +error: + result = NULL; +finally: + Py_XDECREF(tail); + free(buf); + return result; } PyObject * @@ -2555,7 +2610,12 @@ { PyObject *modules = PyImport_GetModuleDict(); PyObject *m = NULL; + PyObject *path, *loader = NULL; + char *buf; + struct filedescr *fdp; + FILE *fp = NULL; + /* Require: if mod == None: subname == fullname else: mod.__name__ + "." + subname == fullname @@ -2563,45 +2623,46 @@ if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { Py_INCREF(m); + return m; } + + if (mod == Py_None) + path = NULL; else { - PyObject *path, *loader = NULL; - char buf[MAXPATHLEN+1]; - struct filedescr *fdp; - FILE *fp = NULL; - - if (mod == Py_None) - path = NULL; - else { - path = PyObject_GetAttrString(mod, "__path__"); - if (path == NULL) { - PyErr_Clear(); - Py_INCREF(Py_None); - return Py_None; - } - } - - buf[0] = '\0'; - fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1, - &fp, &loader); - Py_XDECREF(path); - if (fdp == NULL) { - if (!PyErr_ExceptionMatches(PyExc_ImportError)) - return NULL; + path = PyObject_GetAttrString(mod, "__path__"); + if (path == NULL) { PyErr_Clear(); Py_INCREF(Py_None); return Py_None; } - m = load_module(fullname, fp, buf, fdp->type, loader); - Py_XDECREF(loader); - if (fp) - fclose(fp); - if (!add_submodule(mod, m, fullname, subname, modules)) { - Py_XDECREF(m); - m = NULL; - } } + buf = (char*)malloc(MAXPATHLEN+1); + if (buf == NULL) { + PyErr_NoMemory(); + 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(); + Py_INCREF(Py_None); + return Py_None; + } + m = load_module(fullname, fp, buf, fdp->type, loader); + Py_XDECREF(loader); + if (fp) + fclose(fp); + if (!add_submodule(mod, m, fullname, subname, modules)) { + Py_XDECREF(m); + m = NULL; + } + free(buf); return m; } Index: Python/marshal.c =================================================================== --- Python/marshal.c (révision 80303) +++ Python/marshal.c (copie de travail) @@ -1126,23 +1126,13 @@ PyObject * PyMarshal_ReadLastObjectFromFile(FILE *fp) { -/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT. - * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. - */ -#define SMALL_FILE_LIMIT (1L << 14) +/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */ #define REASONABLE_FILE_LIMIT (1L << 18) #ifdef HAVE_FSTAT off_t filesize; -#endif -#ifdef HAVE_FSTAT filesize = getfilesize(fp); - if (filesize > 0) { - char buf[SMALL_FILE_LIMIT]; - char* pBuf = NULL; - if (filesize <= SMALL_FILE_LIMIT) - pBuf = buf; - else if (filesize <= REASONABLE_FILE_LIMIT) - pBuf = (char *)PyMem_MALLOC(filesize); + if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) { + char* pBuf = (char *)PyMem_MALLOC(filesize); if (pBuf != NULL) { PyObject* v; size_t n; @@ -1150,8 +1140,7 @@ is smaller than REASONABLE_FILE_LIMIT */ n = fread(pBuf, 1, (int)filesize, fp); v = PyMarshal_ReadObjectFromString(pBuf, n); - if (pBuf != buf) - PyMem_FREE(pBuf); + PyMem_FREE(pBuf); return v; } @@ -1162,7 +1151,6 @@ */ return PyMarshal_ReadObjectFromFile(fp); -#undef SMALL_FILE_LIMIT #undef REASONABLE_FILE_LIMIT } Index: Modules/zipimport.c =================================================================== --- Modules/zipimport.c (révision 80303) +++ Modules/zipimport.c (copie de travail) @@ -60,7 +60,7 @@ static int zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) { - char *path, *p, *prefix, buf[MAXPATHLEN+2]; + char *path, *p, *prefix, *buf; size_t len; if (!_PyArg_NoKeywords("zipimporter()", kwds)) @@ -80,6 +80,13 @@ "archive path too long"); return -1; } + + buf = (char*)malloc(MAXPATHLEN+2); + if (buf == NULL) { + PyErr_NoMemory(); + goto error; + } + strcpy(buf, path); #ifdef ALTSEP @@ -128,10 +135,10 @@ if (files == NULL) { files = read_directory(buf); if (files == NULL) - return -1; + goto error; if (PyDict_SetItemString(zip_directory_cache, path, files) != 0) - return -1; + goto error; } else Py_INCREF(files); @@ -139,7 +146,7 @@ } else { PyErr_SetString(ZipImportError, "not a Zip file"); - return -1; + goto error; } if (prefix == NULL) @@ -156,13 +163,18 @@ self->archive = PyString_FromString(buf); if (self->archive == NULL) - return -1; + goto error; self->prefix = PyString_FromString(prefix); if (self->prefix == NULL) - return -1; + goto error; + free(buf); return 0; + +error: + free(buf); + return -1; } /* GC support. */