diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2021-12-20 15:43:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-20 15:43:26 (GMT) |
commit | e9898bf153d26059261ffef11f7643ae991e2a4c (patch) | |
tree | 07923519cbb9265c39c1d7139ae725b4db7ac586 /Tools | |
parent | 6ca78affc8023bc5023189d64d8050857662042a (diff) | |
download | cpython-e9898bf153d26059261ffef11f7643ae991e2a4c.zip cpython-e9898bf153d26059261ffef11f7643ae991e2a4c.tar.gz cpython-e9898bf153d26059261ffef11f7643ae991e2a4c.tar.bz2 |
bpo-46110: Add a recursion check to avoid stack overflow in the PEG parser (GH-30177)
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
Diffstat (limited to 'Tools')
-rw-r--r-- | Tools/peg_generator/pegen/c_generator.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Tools/peg_generator/pegen/c_generator.py b/Tools/peg_generator/pegen/c_generator.py index 9cfbf38..ee255c8 100644 --- a/Tools/peg_generator/pegen/c_generator.py +++ b/Tools/peg_generator/pegen/c_generator.py @@ -37,6 +37,8 @@ EXTENSION_PREFIX = """\ # define D(x) #endif +# define MAXSTACK 6000 + """ @@ -364,10 +366,14 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): self.skip_actions = skip_actions def add_level(self) -> None: - self.print("D(p->level++);") + self.print("if (p->level++ == MAXSTACK) {") + with self.indent(): + self.print("p->error_indicator = 1;") + self.print("PyErr_NoMemory();") + self.print("}") def remove_level(self) -> None: - self.print("D(p->level--);") + self.print("p->level--;") def add_return(self, ret_val: str) -> None: self.remove_level() @@ -544,9 +550,10 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): self.print("p->in_raw_rule++;") self.print(f"void *_raw = {node.name}_raw(p);") self.print("p->in_raw_rule--;") - self.print("if (p->error_indicator)") + self.print("if (p->error_indicator) {") with self.indent(): - self.print("return NULL;") + self.add_return("NULL") + self.print("}") self.print("if (_raw == NULL || p->mark <= _resmark)") with self.indent(): self.print("break;") |