]> granicus.if.org Git - python/commitdiff
Handle more memory allocation failures without crashing.
authorNeal Norwitz <nnorwitz@gmail.com>
Fri, 21 Jul 2006 07:59:47 +0000 (07:59 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Fri, 21 Jul 2006 07:59:47 +0000 (07:59 +0000)
Python/ast.c
Python/compile.c
Python/pythonrun.c
Python/symtable.c
Python/thread.c

index d00fcc8bdd0f681902502a4542dcb4c194faaabb..9180fd0d693fe176e0894fa8e37a6d4b40f1acdc 100644 (file)
@@ -638,8 +638,10 @@ ast_for_arguments(struct compiling *c, const node *n)
                    anything other than EQUAL or a comma? */
                 /* XXX Should NCH(n) check be made a separate check? */
                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
-                    asdl_seq_SET(defaults, j++, 
-                                   ast_for_expr(c, CHILD(n, i + 2)));
+                    expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
+                    if (!expression)
+                            goto error;
+                    asdl_seq_SET(defaults, j++, expression);
                     i += 2;
                    found_default = 1;
                 }
index 8341cc93bbb6e8f495531307573b1f5346d4b22f..3ee5cbbd6e93e21be45e910d3040522772697c70 100644 (file)
@@ -1105,8 +1105,17 @@ compiler_enter_scope(struct compiler *c, identifier name, void *key,
        u->u_name = name;
        u->u_varnames = list2dict(u->u_ste->ste_varnames);
        u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
+       if (!u->u_varnames || !u->u_cellvars) {
+               compiler_unit_free(u);
+               return 0;
+       }
+
        u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
                                   PyDict_Size(u->u_cellvars));
+       if (!u->u_freevars) {
+               compiler_unit_free(u);
+               return 0;
+       }
 
        u->u_blocks = NULL;
        u->u_tmpname = 0;
index 95a337269b79f169e6789d9d73efcfbc932a2ecf..bc832195ecb0692b9f0953ac7d7da1f00e081f19 100644 (file)
@@ -1204,8 +1204,12 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
 {
        PyObject *ret = NULL;
        PyArena *arena = PyArena_New();
-       mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags,
-                                           arena);
+       mod_ty mod;
+
+       if (arena == NULL)
+               return NULL;
+       
+       mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
        if (mod != NULL)
                ret = run_mod(mod, "<string>", globals, locals, flags, arena);
        PyArena_Free(arena);
@@ -1218,8 +1222,13 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
 {
        PyObject *ret;
        PyArena *arena = PyArena_New();
-       mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
-                                         flags, NULL, arena);
+       mod_ty mod;
+
+       if (arena == NULL)
+               return NULL;
+       
+       mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
+                                  flags, NULL, arena);
        if (mod == NULL) {
                PyArena_Free(arena);
                return NULL;
index 1dc2a2ea7466b04f1527ba907b125af93fa0fe7c..fae9208a66e8f387e6f7565c696f0120b9fd3862 100644 (file)
@@ -221,8 +221,12 @@ PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
                return st;
        st->st_filename = filename;
        st->st_future = future;
-       symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock, 
-                            (void *)mod, 0);
+       if (!symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock, 
+                            (void *)mod, 0)) {
+               PySymtable_Free(st);
+               return NULL;
+       }
+
        st->st_top = st->st_cur;
        st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
        /* Any other top-level initialization? */
@@ -728,6 +732,8 @@ symtable_exit_block(struct symtable *st, void *ast)
        if (end >= 0) {
                st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, 
                                                                end);
+               if (st->st_cur == NULL)
+                       return 0;
                Py_INCREF(st->st_cur);
                if (PySequence_DelItem(st->st_stack, end) < 0)
                        return 0;
@@ -749,6 +755,8 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
                Py_DECREF(st->st_cur);
        }
        st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
+       if (st->st_cur == NULL)
+               return 0;
        if (name == GET_IDENTIFIER(top))
                st->st_global = st->st_cur->ste_symbols;
        if (prev) {
index bc501822c0ec51cc9483346c8dc38cab96040d18..3a2c7af6ff6f05501dbba7ef9369b8af70ea9cd8 100644 (file)
@@ -267,6 +267,8 @@ find_key(int key, void *value)
        struct key *p;
        long id = PyThread_get_thread_ident();
 
+       if (!keymutex)
+               return NULL;
        PyThread_acquire_lock(keymutex, 1);
        for (p = keyhead; p != NULL; p = p->next) {
                if (p->id == id && p->key == key)