gh-108179: Add error message for parser stack overflows (#108256)

This commit is contained in:
Dennis Sweeney 2023-08-22 03:41:50 -04:00 committed by GitHub
parent 7f87ebbc3f
commit 86617518c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 542 additions and 1063 deletions

View File

@ -2335,7 +2335,7 @@ while 1:
source = "-" * 100000 + "4"
for mode in ["exec", "eval", "single"]:
with self.subTest(mode=mode):
with self.assertRaises(MemoryError):
with self.assertRaisesRegex(MemoryError, r"too complex"):
compile(source, "<string>", mode)
@support.cpython_only

1590
Parser/parser.c generated

File diff suppressed because it is too large Load Diff

View File

@ -166,6 +166,8 @@ void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t end_lineno, Py_ssize_t end_col_offset,
const char *errmsg, va_list va);
void _Pypegen_set_syntax_error(Parser* p, Token* last_token);
void _Pypegen_stack_overflow(Parser *p);
Py_LOCAL_INLINE(void *)
RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
Py_ssize_t lineno, Py_ssize_t col_offset,

View File

@ -454,3 +454,11 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
// generic SyntaxError we just raised if errors are found.
_PyPegen_tokenize_full_source_to_check_for_errors(p);
}
void
_Pypegen_stack_overflow(Parser *p)
{
p->error_indicator = 1;
PyErr_SetString(PyExc_MemoryError,
"Parser stack overflowed - Python source too complex to parse");
}

View File

@ -375,8 +375,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor):
def add_level(self) -> None:
self.print("if (p->level++ == MAXSTACK) {")
with self.indent():
self.print("p->error_indicator = 1;")
self.print("PyErr_NoMemory();")
self.print("_Pypegen_stack_overflow(p);")
self.print("}")
def remove_level(self) -> None: