summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-03-02 10:36:38 (GMT)
committerGitHub <noreply@github.com>2021-03-02 10:36:38 (GMT)
commit8b795ab5541d8a4e69be4137dfdc207714270b77 (patch)
treed7fcc96f3243d082ea024964a643a32a004a5fbd /Python
parentf836e5f2194857b24ec03adfcfcce05375868f88 (diff)
downloadcpython-8b795ab5541d8a4e69be4137dfdc207714270b77.zip
cpython-8b795ab5541d8a4e69be4137dfdc207714270b77.tar.gz
cpython-8b795ab5541d8a4e69be4137dfdc207714270b77.tar.bz2
bpo-42500: Fix recursion in or after except (GH-23568) (#24501)
* Use counter, rather boolean state when handling soft overflows. (cherry picked from commit 4e7a69bdb63a104587759d7784124492dcdd496e)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c23
-rw-r--r--Python/errors.c3
-rw-r--r--Python/pystate.c2
-rw-r--r--Python/sysmodule.c4
4 files changed, 16 insertions, 16 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 91e879e..b7176dc 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -793,23 +793,22 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
_Py_CheckRecursionLimit = recursion_limit;
}
#endif
- if (tstate->recursion_critical)
- /* Somebody asked that we don't check for recursion. */
- return 0;
- if (tstate->overflowed) {
+ if (tstate->recursion_headroom) {
if (tstate->recursion_depth > recursion_limit + 50) {
/* Overflowing while handling an overflow. Give up. */
Py_FatalError("Cannot recover from stack overflow.");
}
- return 0;
}
- if (tstate->recursion_depth > recursion_limit) {
- --tstate->recursion_depth;
- tstate->overflowed = 1;
- _PyErr_Format(tstate, PyExc_RecursionError,
- "maximum recursion depth exceeded%s",
- where);
- return -1;
+ else {
+ if (tstate->recursion_depth > recursion_limit) {
+ tstate->recursion_headroom++;
+ _PyErr_Format(tstate, PyExc_RecursionError,
+ "maximum recursion depth exceeded%s",
+ where);
+ tstate->recursion_headroom--;
+ --tstate->recursion_depth;
+ return -1;
+ }
}
return 0;
}
diff --git a/Python/errors.c b/Python/errors.c
index 87af39d..d8c2d8b 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -290,12 +290,14 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
PyObject **val, PyObject **tb)
{
int recursion_depth = 0;
+ tstate->recursion_headroom++;
PyObject *type, *value, *initial_tb;
restart:
type = *exc;
if (type == NULL) {
/* There was no exception, so nothing to do. */
+ tstate->recursion_headroom--;
return;
}
@@ -347,6 +349,7 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
}
*exc = type;
*val = value;
+ tstate->recursion_headroom--;
return;
error:
diff --git a/Python/pystate.c b/Python/pystate.c
index 9beefa8..71aac6f 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -576,7 +576,7 @@ new_threadstate(PyInterpreterState *interp, int init)
tstate->frame = NULL;
tstate->recursion_depth = 0;
- tstate->overflowed = 0;
+ tstate->recursion_headroom = 0;
tstate->recursion_critical = 0;
tstate->stackcheck_counter = 0;
tstate->tracing = 0;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 3e4115f..4c7f7b6 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1160,7 +1160,6 @@ static PyObject *
sys_setrecursionlimit_impl(PyObject *module, int new_limit)
/*[clinic end generated code: output=35e1c64754800ace input=b0f7a23393924af3]*/
{
- int mark;
PyThreadState *tstate = _PyThreadState_GET();
if (new_limit < 1) {
@@ -1178,8 +1177,7 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit)
Reject too low new limit if the current recursion depth is higher than
the new low-water mark. Otherwise it may not be possible anymore to
reset the overflowed flag to 0. */
- mark = _Py_RecursionLimitLowerWaterMark(new_limit);
- if (tstate->recursion_depth >= mark) {
+ if (tstate->recursion_depth >= new_limit) {
_PyErr_Format(tstate, PyExc_RecursionError,
"cannot set the recursion limit to %i at "
"the recursion depth %i: the limit is too low",