[3.10] bpo-46707: Avoid potential exponential backtracking in some syntax errors (GH-31241). (GH-31242)

(cherry picked from commit b71dc71905)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Pablo Galindo Salgado 2022-02-10 03:54:47 +00:00 committed by GitHub
parent 7445949a43
commit 9b23f8f78f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1298 additions and 1281 deletions

View file

@ -863,13 +863,15 @@ invalid_expression:
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
invalid_left_assignment_prefixes(memo): list|tuple|genexp|'True'|'None'|'False'
invalid_named_expression:
| a=expression ':=' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
| a=NAME '=' b=bitwise_or !('='|':=') {
p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {
| !invalid_left_assignment_prefixes a=bitwise_or b='=' bitwise_or !('='|':=') {
p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
_PyPegen_get_expr_name(a)) }

View file

@ -1617,6 +1617,14 @@ while 1:
with self.assertRaises(MemoryError):
compile(source, "<string>", mode)
@support.cpython_only
def test_deep_invalid_rule(self):
# Check that a very deep invalid rule in the PEG
# parser doesn't have exponential backtracking.
source = "d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():y"
with self.assertRaises(SyntaxError):
compile(source, "<string>", "exec")
def test_main():
support.run_unittest(SyntaxTestCase)

View file

@ -0,0 +1,2 @@
Avoid potential exponential backtracking when producing some syntax errors
involving lots of brackets. Patch by Pablo Galindo.

File diff suppressed because it is too large Load diff