Skip to content

Commit ac08700

Browse files
KowalskiThomasmiss-islington
authored andcommitted
gh-149689: add missing error checks in Parser/action_helpers.c (GH-149710)
(cherry picked from commit 2dd6e59) Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
1 parent 5a09ae2 commit ac08700

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix missing error propagation in parser action helpers when memory allocation
2+
fails. Patch by Thomas Kowalski.

Parser/action_helpers.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ _set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
254254
}
255255
for (Py_ssize_t i = 0; i < len; i++) {
256256
expr_ty e = asdl_seq_GET(seq, i);
257-
asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
257+
expr_ty new_e = _PyPegen_set_expr_context(p, e, ctx);
258+
if (!new_e) {
259+
return NULL;
260+
}
261+
asdl_seq_SET(new_seq, i, new_e);
258262
}
259263
return new_seq;
260264
}
@@ -268,19 +272,21 @@ _set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
268272
static expr_ty
269273
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
270274
{
271-
return _PyAST_Tuple(
272-
_set_seq_context(p, e->v.Tuple.elts, ctx),
273-
ctx,
274-
EXTRA_EXPR(e, e));
275+
asdl_expr_seq *seq = _set_seq_context(p, e->v.Tuple.elts, ctx);
276+
if (!seq && PyErr_Occurred()) {
277+
return NULL;
278+
}
279+
return _PyAST_Tuple(seq, ctx, EXTRA_EXPR(e, e));
275280
}
276281

277282
static expr_ty
278283
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
279284
{
280-
return _PyAST_List(
281-
_set_seq_context(p, e->v.List.elts, ctx),
282-
ctx,
283-
EXTRA_EXPR(e, e));
285+
asdl_expr_seq *seq = _set_seq_context(p, e->v.List.elts, ctx);
286+
if (!seq && PyErr_Occurred()) {
287+
return NULL;
288+
}
289+
return _PyAST_List(seq, ctx, EXTRA_EXPR(e, e));
284290
}
285291

286292
static expr_ty
@@ -300,8 +306,11 @@ _set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
300306
static expr_ty
301307
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
302308
{
303-
return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
304-
ctx, EXTRA_EXPR(e, e));
309+
expr_ty inner = _PyPegen_set_expr_context(p, e->v.Starred.value, ctx);
310+
if (!inner) {
311+
return NULL;
312+
}
313+
return _PyAST_Starred(inner, ctx, EXTRA_EXPR(e, e));
305314
}
306315

307316
/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
@@ -1139,7 +1148,14 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
11391148
}
11401149

11411150
asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b);
1151+
if (!starreds && PyErr_Occurred()) {
1152+
return NULL;
1153+
}
1154+
11421155
asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b);
1156+
if (!keywords && PyErr_Occurred()) {
1157+
return NULL;
1158+
}
11431159

11441160
if (starreds) {
11451161
total_len += asdl_seq_LEN(starreds);
@@ -1551,7 +1567,7 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
15511567
end_col_offset, arena
15521568
);
15531569

1554-
if (!debug) {
1570+
if (!interpolation || !debug) {
15551571
return interpolation;
15561572
}
15571573

@@ -1562,6 +1578,9 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
15621578
}
15631579

15641580
asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1581+
if (!values) {
1582+
return NULL;
1583+
}
15651584
asdl_seq_SET(values, 0, debug_text);
15661585
asdl_seq_SET(values, 1, interpolation);
15671586
return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
@@ -1578,7 +1597,7 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, Re
15781597
end_col_offset, arena
15791598
);
15801599

1581-
if (!debug) {
1600+
if (!formatted_value || !debug) {
15821601
return formatted_value;
15831602
}
15841603

@@ -1608,6 +1627,9 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, Re
16081627
}
16091628

16101629
asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1630+
if (!values) {
1631+
return NULL;
1632+
}
16111633
asdl_seq_SET(values, 0, debug_text);
16121634
asdl_seq_SET(values, 1, formatted_value);
16131635
return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
@@ -1848,6 +1870,9 @@ _build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings,
18481870
{
18491871
asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
18501872
col_offset, end_lineno, end_col_offset, arena);
1873+
if (!values) {
1874+
return NULL;
1875+
}
18511876
return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
18521877
}
18531878

@@ -1858,6 +1883,9 @@ _PyPegen_concatenate_tstrings(Parser *p, asdl_expr_seq *strings,
18581883
{
18591884
asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
18601885
col_offset, end_lineno, end_col_offset, arena);
1886+
if (!values) {
1887+
return NULL;
1888+
}
18611889
return _PyAST_TemplateStr(values, lineno, col_offset, end_lineno,
18621890
end_col_offset, arena);
18631891
}

0 commit comments

Comments
 (0)