aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/symtable.c
diff options
context:
space:
mode:
authorPetr Viktorin <encukou@gmail.com>2025-02-24 11:16:08 +0100
committerGitHub <noreply@github.com>2025-02-24 11:16:08 +0100
commitef29104f7d7ad7864f5f153cd7391af73d7cef63 (patch)
tree38c76b08e114a9e6e993081b828b64978bfefa2e /Python/symtable.c
parent0ff16115741aeaaaf7f963f68d5c575efb960277 (diff)
downloadcpython-ef29104f7d7ad7864f5f153cd7391af73d7cef63.tar.gz
cpython-ef29104f7d7ad7864f5f153cd7391af73d7cef63.zip
GH-91079: Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now (GH130413)
Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now Unfortunatlely, the change broke some buildbots. This reverts commit 2498c22fa0a2b560491bc503fa676585c1a603d0.
Diffstat (limited to 'Python/symtable.c')
-rw-r--r--Python/symtable.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 87357618331..49bd01ba68a 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -406,6 +406,7 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, _PyFutureFeatures *future)
asdl_stmt_seq *seq;
Py_ssize_t i;
PyThreadState *tstate;
+ int starting_recursion_depth;
if (st == NULL)
return NULL;
@@ -422,6 +423,11 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, _PyFutureFeatures *future)
_PySymtable_Free(st);
return NULL;
}
+ /* Be careful here to prevent overflow. */
+ int recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
+ starting_recursion_depth = recursion_depth;
+ st->recursion_depth = starting_recursion_depth;
+ st->recursion_limit = Py_C_RECURSION_LIMIT;
/* Make the initial symbol information gathering pass */
@@ -463,6 +469,14 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, _PyFutureFeatures *future)
_PySymtable_Free(st);
return NULL;
}
+ /* Check that the recursion depth counting balanced correctly */
+ if (st->recursion_depth != starting_recursion_depth) {
+ PyErr_Format(PyExc_SystemError,
+ "symtable analysis recursion depth mismatch (before=%d, after=%d)",
+ starting_recursion_depth, st->recursion_depth);
+ _PySymtable_Free(st);
+ return NULL;
+ }
/* Make the second symbol analysis pass */
if (symtable_analyze(st)) {
#if _PY_DUMP_SYMTABLE
@@ -1722,12 +1736,19 @@ symtable_enter_type_param_block(struct symtable *st, identifier name,
} \
} while(0)
-#define ENTER_RECURSIVE() \
-if (Py_EnterRecursiveCall(" during compilation")) { \
- return 0; \
-}
+#define ENTER_RECURSIVE(ST) \
+ do { \
+ if (++(ST)->recursion_depth > (ST)->recursion_limit) { \
+ PyErr_SetString(PyExc_RecursionError, \
+ "maximum recursion depth exceeded during compilation"); \
+ return 0; \
+ } \
+ } while(0)
-#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
+#define LEAVE_RECURSIVE(ST) \
+ do { \
+ --(ST)->recursion_depth; \
+ } while(0)
static int
@@ -1802,7 +1823,7 @@ maybe_set_ste_coroutine_for_module(struct symtable *st, stmt_ty s)
static int
symtable_visit_stmt(struct symtable *st, stmt_ty s)
{
- ENTER_RECURSIVE();
+ ENTER_RECURSIVE(st);
switch (s->kind) {
case FunctionDef_kind: {
if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL, LOCATION(s)))
@@ -2214,7 +2235,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
break;
}
- LEAVE_RECURSIVE();
+ LEAVE_RECURSIVE(st);
return 1;
}
@@ -2337,7 +2358,7 @@ symtable_handle_namedexpr(struct symtable *st, expr_ty e)
static int
symtable_visit_expr(struct symtable *st, expr_ty e)
{
- ENTER_RECURSIVE();
+ ENTER_RECURSIVE(st);
switch (e->kind) {
case NamedExpr_kind:
if (!symtable_raise_if_annotation_block(st, "named expression", e)) {
@@ -2508,7 +2529,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
VISIT_SEQ(st, expr, e->v.Tuple.elts);
break;
}
- LEAVE_RECURSIVE();
+ LEAVE_RECURSIVE(st);
return 1;
}
@@ -2542,7 +2563,7 @@ symtable_visit_type_param_bound_or_default(
static int
symtable_visit_type_param(struct symtable *st, type_param_ty tp)
{
- ENTER_RECURSIVE();
+ ENTER_RECURSIVE(st);
switch(tp->kind) {
case TypeVar_kind:
if (!symtable_add_def(st, tp->v.TypeVar.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
@@ -2591,14 +2612,14 @@ symtable_visit_type_param(struct symtable *st, type_param_ty tp)
}
break;
}
- LEAVE_RECURSIVE();
+ LEAVE_RECURSIVE(st);
return 1;
}
static int
symtable_visit_pattern(struct symtable *st, pattern_ty p)
{
- ENTER_RECURSIVE();
+ ENTER_RECURSIVE(st);
switch (p->kind) {
case MatchValue_kind:
VISIT(st, expr, p->v.MatchValue.value);
@@ -2647,7 +2668,7 @@ symtable_visit_pattern(struct symtable *st, pattern_ty p)
VISIT_SEQ(st, pattern, p->v.MatchOr.patterns);
break;
}
- LEAVE_RECURSIVE();
+ LEAVE_RECURSIVE(st);
return 1;
}