summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorLysandros Nikolaou <lisandrosnik@gmail.com>2020-05-18 17:32:03 (GMT)
committerGitHub <noreply@github.com>2020-05-18 17:32:03 (GMT)
commit7b7a21bc4fd063b26a2d1882fddc458861497812 (patch)
tree31eb7583108b66930b3937546d786d2c9b0328d6 /Tools
parent08b47c367a08f571a986366aa33828d3951fa88d (diff)
downloadcpython-7b7a21bc4fd063b26a2d1882fddc458861497812.zip
cpython-7b7a21bc4fd063b26a2d1882fddc458861497812.tar.gz
cpython-7b7a21bc4fd063b26a2d1882fddc458861497812.tar.bz2
bpo-40661: Fix segfault when parsing invalid input (GH-20165)
Fix segfaults when parsing very complex invalid input, like `import äˆ ð£„¯ð¢·žð±‹á”€ð””ð‘©±å®ä±¬ð©¾\n𗶽`. Co-authored-by: Guido van Rossum <guido@python.org> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Diffstat (limited to 'Tools')
-rw-r--r--Tools/peg_generator/pegen/c_generator.py17
1 files changed, 9 insertions, 8 deletions
diff --git a/Tools/peg_generator/pegen/c_generator.py b/Tools/peg_generator/pegen/c_generator.py
index 8f9972b..c93b348 100644
--- a/Tools/peg_generator/pegen/c_generator.py
+++ b/Tools/peg_generator/pegen/c_generator.py
@@ -433,6 +433,12 @@ class CParserGenerator(ParserGenerator, GrammarVisitor):
self.print("int _end_col_offset = _token->end_col_offset;")
self.print("UNUSED(_end_col_offset); // Only used by EXTRA macro")
+ def _check_for_errors(self) -> None:
+ self.print("if (p->error_indicator) {")
+ with self.indent():
+ self.print("return NULL;")
+ self.print("}")
+
def _set_up_rule_memoization(self, node: Rule, result_type: str) -> None:
self.print("{")
with self.indent():
@@ -468,10 +474,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor):
memoize = self._should_memoize(node)
with self.indent():
- self.print("if (p->error_indicator) {")
- with self.indent():
- self.print("return NULL;")
- self.print("}")
+ self._check_for_errors()
self.print(f"{result_type} _res = NULL;")
if memoize:
self.print(f"if (_PyPegen_is_memoized(p, {node.name}_type, &_res))")
@@ -500,10 +503,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor):
is_repeat1 = node.name.startswith("_loop1")
with self.indent():
- self.print("if (p->error_indicator) {")
- with self.indent():
- self.print("return NULL;")
- self.print("}")
+ self._check_for_errors()
self.print("void *_res = NULL;")
if memoize:
self.print(f"if (_PyPegen_is_memoized(p, {node.name}_type, &_res))")
@@ -687,6 +687,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor):
) -> None:
self.print(f"{{ // {node}")
with self.indent():
+ self._check_for_errors()
# Prepare variable declarations for the alternative
vars = self.collect_vars(node)
for v, var_type in sorted(item for item in vars.items() if item[0] is not None):