Skip to content

[3.9] bpo-42218: Correctly handle errors in left-recursive rules (GH-23065) #23066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,14 @@ def func2():
"""
self._check_error(code, "invalid syntax")

def test_invalid_line_continuation_left_recursive(self):
# Check bpo-42218: SyntaxErrors following left-recursive rules
# (t_primary_raw in this case) need to be tested explicitly
self._check_error("A.\u018a\\ ",
"unexpected character after line continuation character")
self._check_error("A.\u03bc\\\n",
"unexpected EOF while parsing")

def test_main():
support.run_unittest(SyntaxTestCase)
from test import test_syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a bug in the PEG parser that was causing crashes in debug mode. Now errors are checked
in left-recursive rules to avoid cases where such errors do not get handled in time and appear
as long-distance crashes in other places.
18 changes: 18 additions & 0 deletions Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,8 @@ dotted_name_rule(Parser *p)
}
p->mark = _mark;
void *_raw = dotted_name_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9044,6 +9046,8 @@ bitwise_or_rule(Parser *p)
}
p->mark = _mark;
void *_raw = bitwise_or_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9158,6 +9162,8 @@ bitwise_xor_rule(Parser *p)
}
p->mark = _mark;
void *_raw = bitwise_xor_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9272,6 +9278,8 @@ bitwise_and_rule(Parser *p)
}
p->mark = _mark;
void *_raw = bitwise_and_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9386,6 +9394,8 @@ shift_expr_rule(Parser *p)
}
p->mark = _mark;
void *_raw = shift_expr_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9539,6 +9549,8 @@ sum_rule(Parser *p)
}
p->mark = _mark;
void *_raw = sum_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -9698,6 +9710,8 @@ term_rule(Parser *p)
}
p->mark = _mark;
void *_raw = term_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -10302,6 +10316,8 @@ primary_rule(Parser *p)
}
p->mark = _mark;
void *_raw = primary_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down Expand Up @@ -13962,6 +13978,8 @@ t_primary_rule(Parser *p)
}
p->mark = _mark;
void *_raw = t_primary_raw(p);
if (p->error_indicator)
return NULL;
if (_raw == NULL || p->mark <= _resmark)
break;
_resmark = p->mark;
Expand Down
3 changes: 3 additions & 0 deletions Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ def _set_up_rule_memoization(self, node: Rule, result_type: str) -> None:
)
self.print("p->mark = _mark;")
self.print(f"void *_raw = {node.name}_raw(p);")
self.print("if (p->error_indicator)")
with self.indent():
self.print("return NULL;")
self.print("if (_raw == NULL || p->mark <= _resmark)")
with self.indent():
self.print("break;")
Expand Down