Benjamin Peterson avatar Benjamin Peterson committed 2d3f1d0

backport r67373: checking for errors from NEW_IDENTIFIER

Comments (0)

Files changed (1)

 static identifier
 new_identifier(const char* n, PyArena *arena) {
     PyObject* id = PyString_InternFromString(n);
-    PyArena_AddPyObject(arena, id);
+    if (id != NULL)
+        PyArena_AddPyObject(arena, id);
     return id;
 }
 
     */
     REQ(n, fplist);
     for (i = 0; i < len; i++) {
+        PyObject *arg_id;
         const node *fpdef_node = CHILD(n, 2*i);
         const node *child;
         expr_ty arg;
         /* fpdef_node is either a NAME or an fplist */
         child = CHILD(fpdef_node, 0);
         if (TYPE(child) == NAME) {
-    		if (!strcmp(STR(child), "None")) {
-	    		ast_error(child, "assignment to None");
-		    	return NULL;
-		    }   
-            arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
-                       child->n_col_offset, c->c_arena);
-	    }
+            if (!strcmp(STR(child), "None")) {
+                    ast_error(child, "assignment to None");
+                    return NULL;
+            }   
+            arg_id = NEW_IDENTIFIER(child);
+            if (!arg_id)
+                return NULL;
+            arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
+                       c->c_arena);
+        }
         else {
             assert(TYPE(fpdef_node) == fpdef);
             /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
 		    }
                 }
                 if (TYPE(CHILD(ch, 0)) == NAME) {
+                    PyObject *id;
 		    expr_ty name;
 		    if (!strcmp(STR(CHILD(ch, 0)), "None")) {
 			    ast_error(CHILD(ch, 0), "assignment to None");
 			    goto error;
 		    }
-                    name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
-                                Param, LINENO(ch), ch->n_col_offset,
+                    id = NEW_IDENTIFIER(CHILD(ch, 0));
+                    if (!id)
+                        goto error;
+                    name = Name(id, Param, LINENO(ch), ch->n_col_offset,
                                 c->c_arena);
                     if (!name)
                         goto error;
 			goto error;
 		}
                 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
+                if (!vararg)
+                    goto error;
                 i += 3;
                 break;
             case DOUBLESTAR:
 			goto error;
 		}
                 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
+                if (!kwarg)
+                    goto error;
                 i += 3;
                 break;
             default:
     node *ch = CHILD(n, 0);
     
     switch (TYPE(ch)) {
-    case NAME:
-	/* All names start in Load context, but may later be
-	   changed. */
-	return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
+    case NAME: {
+        /* All names start in Load context, but may later be
+           changed. */
+        PyObject *id = NEW_IDENTIFIER(ch);
+        if (!id)
+            return NULL;
+        return Name(id, Load, LINENO(n), n->n_col_offset, c->c_arena);
+    }
     case STRING: {
 	PyObject *str = parsestrplus(c, n);
 	if (!str)
             return ast_for_call(c, CHILD(n, 1), left_expr);
     }
     else if (TYPE(CHILD(n, 0)) == DOT ) {
-        return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
+        PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
+        if (!attr_id)
+            return NULL;
+        return Attribute(left_expr, attr_id, Load,
                          LINENO(n), n->n_col_offset, c->c_arena);
     }
     else {
       dotted_as_name: dotted_name ['as' NAME]
       dotted_name: NAME ('.' NAME)*
     */
-    PyObject *str;
+    PyObject *str, *name;
 
  loop:
     switch (TYPE(n)) {
                     return NULL;
                 }
                 str = NEW_IDENTIFIER(CHILD(n, 2));
+                if (!str)
+                    return NULL;
             }
-            return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
+            name = NEW_IDENTIFIER(CHILD(n, 0));
+            if (!name)
+                return NULL;
+            return alias(name, str, c->c_arena);
         case dotted_as_name:
             if (NCH(n) == 1) {
                 n = CHILD(n, 0);
                 }
                 assert(!a->asname);
                 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
+                if (!a->asname)
+                    return NULL;
                 return a;
             }
             break;
         case dotted_name:
-            if (NCH(n) == 1)
-                return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
+            if (NCH(n) == 1) {
+                name = NEW_IDENTIFIER(CHILD(n, 0));
+                if (!name)
+                    return NULL;
+                return alias(name, NULL, c->c_arena);
+            }
             else {
                 /* Create a string of the form "a.b.c" */
                 int i;
 ast_for_classdef(struct compiling *c, const node *n)
 {
     /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
+    PyObject *classname;
     asdl_seq *bases, *s;
     
     REQ(n, classdef);
         s = ast_for_suite(c, CHILD(n, 3));
         if (!s)
             return NULL;
-	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
+        classname = NEW_IDENTIFIER(CHILD(n, 1));
+        if (!classname)
+            return NULL;
+        return ClassDef(classname, NULL, s, LINENO(n),
                         n->n_col_offset, c->c_arena);
     }
     /* check for empty base list */
     if (TYPE(CHILD(n,3)) == RPAR) {
-	s = ast_for_suite(c, CHILD(n,5));
-	if (!s)
-		return NULL;
-	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
+        s = ast_for_suite(c, CHILD(n, 5));
+        if (!s)
+            return NULL;
+        classname = NEW_IDENTIFIER(CHILD(n, 1));
+        if (!classname)
+            return NULL;
+	return ClassDef(classname, NULL, s, LINENO(n),
                         n->n_col_offset, c->c_arena);
     }
 
     s = ast_for_suite(c, CHILD(n, 6));
     if (!s)
         return NULL;
-    return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
+    classname = NEW_IDENTIFIER(CHILD(n, 1));
+    if (!classname)
+        return NULL;
+    return ClassDef(classname, bases, s, LINENO(n),
                     n->n_col_offset, c->c_arena);
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.