Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 76511c0

Browse files
committed
Check NEW_TYPE_COMMENT calls for failure
This is mostly cribbed from python/cpython#11645, though it also adds a new error check to new_type_comment itself.
1 parent c6d7fcb commit 76511c0

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

ast3/Python/ast.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ static string
741741
new_type_comment(const char *s, struct compiling *c)
742742
{
743743
PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
744+
if (!res)
745+
return NULL;
744746
if (PyArena_AddPyObject(c->c_arena, res) < 0) {
745747
Py_DECREF(res);
746748
return NULL;
@@ -1449,6 +1451,8 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
14491451
case TYPE_COMMENT:
14501452
/* arg will be equal to the last argument processed */
14511453
arg->type_comment = NEW_TYPE_COMMENT(ch);
1454+
if (!arg->type_comment)
1455+
goto error;
14521456
i += 1;
14531457
break;
14541458
case DOUBLESTAR:
@@ -1618,6 +1622,8 @@ ast_for_arguments(struct compiling *c, const node *n)
16181622

16191623
if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
16201624
vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1625+
if (!vararg->type_comment)
1626+
return NULL;
16211627
i += 1;
16221628
}
16231629

@@ -1649,6 +1655,8 @@ ast_for_arguments(struct compiling *c, const node *n)
16491655

16501656
/* arg will be equal to the last argument processed */
16511657
arg->type_comment = NEW_TYPE_COMMENT(ch);
1658+
if (!arg->type_comment)
1659+
return NULL;
16521660
i += 1;
16531661
break;
16541662
default:
@@ -1788,6 +1796,8 @@ ast_for_funcdef_impl(struct compiling *c, const node *n0,
17881796
}
17891797
if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
17901798
type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1799+
if (!type_comment)
1800+
return NULL;
17911801
name_i += 1;
17921802
}
17931803
body = ast_for_suite(c, CHILD(n, name_i + 3));
@@ -1804,6 +1814,8 @@ ast_for_funcdef_impl(struct compiling *c, const node *n0,
18041814
return NULL;
18051815
}
18061816
type_comment = NEW_TYPE_COMMENT(tc);
1817+
if (!type_comment)
1818+
return NULL;
18071819
}
18081820
}
18091821

@@ -3290,8 +3302,11 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
32903302
expression = ast_for_expr(c, value);
32913303
if (!expression)
32923304
return NULL;
3293-
if (has_type_comment)
3305+
if (has_type_comment) {
32943306
type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3307+
if (!type_comment)
3308+
return NULL;
3309+
}
32953310
else
32963311
type_comment = NULL;
32973312
return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, c->c_arena);
@@ -3979,8 +3994,11 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
39793994
if (!suite_seq)
39803995
return NULL;
39813996

3982-
if (has_type_comment)
3997+
if (has_type_comment) {
39833998
type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
3999+
if (!type_comment)
4000+
return NULL;
4001+
}
39844002
else
39854003
type_comment = NULL;
39864004

@@ -4171,8 +4189,11 @@ ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
41714189
if (!body)
41724190
return NULL;
41734191

4174-
if (has_type_comment)
4192+
if (has_type_comment) {
41754193
type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4194+
if (!type_comment)
4195+
return NULL;
4196+
}
41764197
else
41774198
type_comment = NULL;
41784199

0 commit comments

Comments
 (0)