]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-135028: Increase parser MAXSTACK for nested parenthesis (#135031)
authorVictor Stinner <vstinner@python.org>
Tue, 3 Jun 2025 06:40:45 +0000 (08:40 +0200)
committerGitHub <noreply@github.com>
Tue, 3 Jun 2025 06:40:45 +0000 (08:40 +0200)
Lib/test/test_grammar.py
Parser/parser.c
Tools/peg_generator/pegen/c_generator.py

index c39565144bf7f404ad8e3c5f6d273ae1aa580444..7f5d48b9c63ab7d349df5ab68ca03eceab21a20c 100644 (file)
@@ -1,7 +1,7 @@
 # Python test set -- part 1, grammar.
 # This just tests whether the parser accepts them all.
 
-from test.support import check_syntax_error
+from test.support import check_syntax_error, skip_wasi_stack_overflow
 from test.support import import_helper
 import annotationlib
 import inspect
@@ -249,6 +249,18 @@ the \'lazy\' dog.\n\
                 compile(s, "<test>", "exec")
             self.assertIn("was never closed", str(cm.exception))
 
+    @skip_wasi_stack_overflow()
+    def test_max_level(self):
+        # Macro defined in Parser/lexer/state.h
+        MAXLEVEL = 200
+
+        result = eval("(" * MAXLEVEL + ")" * MAXLEVEL)
+        self.assertEqual(result, ())
+
+        with self.assertRaises(SyntaxError) as cm:
+            eval("(" * (MAXLEVEL + 1) + ")" * (MAXLEVEL + 1))
+        self.assertStartsWith(str(cm.exception), 'too many nested parentheses')
+
 var_annot_global: int # a global annotated is necessary for test_var_annot
 
 
index 84a293cddffde65214c77c3acf7bb248bf0b9bd4..d5aafef826ed3af45a7662938703fa536e84bba9 100644 (file)
@@ -14,7 +14,7 @@
 #    define MAXSTACK 4000
 #  endif
 #else
-#  define MAXSTACK 4000
+#  define MAXSTACK 6000
 #endif
 static const int n_keyword_lists = 9;
 static KeywordToken *reserved_keywords[] = {
index 2be85a163b4043e6bc3009e05c76f6ac162872f9..09c5651f24a3bbd12a9f523f30b95315c5c5a1a3 100644 (file)
@@ -44,7 +44,7 @@ EXTENSION_PREFIX = """\
 #    define MAXSTACK 4000
 #  endif
 #else
-#  define MAXSTACK 4000
+#  define MAXSTACK 6000
 #endif
 
 """