]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-113297: Fix segfault in compiler for with statement with 19 context manager...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sat, 23 Dec 2023 13:29:11 +0000 (13:29 +0000)
committerGitHub <noreply@github.com>
Sat, 23 Dec 2023 13:29:11 +0000 (13:29 +0000)
Include/internal/pycore_flowgraph.h
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2023-12-20-18-27-11.gh-issue-113297.BZyAI_.rst [new file with mode: 0644]
Python/flowgraph.c

index 720feb186369595ae7bb291cfbf98721d43dc5a1..98d3374a752202fcaef409e4339d8707469edb06 100644 (file)
@@ -26,7 +26,7 @@ typedef struct {
 
 
 typedef struct {
-    struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+1];
+    struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+2];
     int depth;
 } _PyCfgExceptStack;
 
index 584726538c02ca7e82d41269aa6d06ce85238686..fe3ea3d248b0ed969e094e5e706ce4cfb14bd583 100644 (file)
@@ -1995,6 +1995,7 @@ Invalid expressions in type scopes:
 
 import re
 import doctest
+import textwrap
 import unittest
 
 from test import support
@@ -2241,6 +2242,31 @@ if x:
         code += f"{' '*4*12}pass"
         self._check_error(code, "too many statically nested blocks")
 
+    @support.cpython_only
+    def test_with_statement_many_context_managers(self):
+        # See gh-113297
+
+        def get_code(n):
+            code = textwrap.dedent("""
+                def bug():
+                    with (
+                    a
+                """)
+            for i in range(n):
+                code += f"    as a{i}, a\n"
+            code += "): yield a"
+            return code
+
+        CO_MAXBLOCKS = 20  # static nesting limit of the compiler
+
+        for n in range(CO_MAXBLOCKS):
+            with self.subTest(f"within range: {n=}"):
+                compile(get_code(n), "<string>", "exec")
+
+        for n in range(CO_MAXBLOCKS, CO_MAXBLOCKS + 5):
+            with self.subTest(f"out of range: {n=}"):
+                self._check_error(get_code(n), "too many statically nested blocks")
+
     def test_barry_as_flufl_with_syntax_errors(self):
         # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
         # is reading the wrong token in the presence of syntax errors later
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-20-18-27-11.gh-issue-113297.BZyAI_.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-20-18-27-11.gh-issue-113297.BZyAI_.rst
new file mode 100644 (file)
index 0000000..b6aee1f
--- /dev/null
@@ -0,0 +1 @@
+Fix segfault in the compiler on with statement with 19 context managers.
index b53e771bb7bb1e40675bcf8291b14d5335a4568f..fbbe053ae58e9732ad3ed2682ac3cdaec2ea3a61 100644 (file)
@@ -645,6 +645,7 @@ push_except_block(ExceptStack *stack, cfg_instr *setup) {
     if (opcode == SETUP_WITH || opcode == SETUP_CLEANUP) {
         target->b_preserve_lasti = 1;
     }
+    assert(stack->depth <= CO_MAXBLOCKS);
     stack->handlers[++stack->depth] = target;
     return target;
 }