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

Commit f2acb2b

Browse files
msullivangvanrossum
authored andcommitted
Check NEW_TYPE_COMMENT calls for failure (#93)
This is mostly cribbed from python/cpython#11645, though it also adds a new error check to new_type_comment itself.
1 parent 19d2c62 commit f2acb2b

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

@@ -3286,8 +3298,11 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
32863298
expression = ast_for_expr(c, value);
32873299
if (!expression)
32883300
return NULL;
3289-
if (has_type_comment)
3301+
if (has_type_comment) {
32903302
type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3303+
if (!type_comment)
3304+
return NULL;
3305+
}
32913306
else
32923307
type_comment = NULL;
32933308
return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, c->c_arena);
@@ -3975,8 +3990,11 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
39753990
if (!suite_seq)
39763991
return NULL;
39773992

3978-
if (has_type_comment)
3993+
if (has_type_comment) {
39793994
type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
3995+
if (!type_comment)
3996+
return NULL;
3997+
}
39803998
else
39813999
type_comment = NULL;
39824000

@@ -4167,8 +4185,11 @@ ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
41674185
if (!body)
41684186
return NULL;
41694187

4170-
if (has_type_comment)
4188+
if (has_type_comment) {
41714189
type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4190+
if (!type_comment)
4191+
return NULL;
4192+
}
41724193
else
41734194
type_comment = NULL;
41744195

0 commit comments

Comments
 (0)