diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-07-05 18:52:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-05 18:52:33 (GMT) |
commit | 5e24c80b948da6995990296cf262d9eae265e8ec (patch) | |
tree | ca0c20c8012eea7e14d34c7f1488587b88977b05 /Python | |
parent | fd34bfe48444fdb22ff1ae78941cf621854b351f (diff) | |
download | cpython-5e24c80b948da6995990296cf262d9eae265e8ec.zip cpython-5e24c80b948da6995990296cf262d9eae265e8ec.tar.gz cpython-5e24c80b948da6995990296cf262d9eae265e8ec.tar.bz2 |
[3.10] gh-94510: Raise on re-entrant calls to sys.setprofile and syssettrace (GH-94511) (#94579)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>.
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 9a193c9..df4b9a8 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -5525,10 +5525,20 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) /* The caller must hold the GIL */ assert(PyGILState_Check()); + static int reentrant = 0; + if (reentrant) { + _PyErr_SetString(tstate, PyExc_RuntimeError, "Cannot install a profile function " + "while another profile function is being installed"); + reentrant = 0; + return -1; + } + reentrant = 1; + /* Call _PySys_Audit() in the context of the current thread state, even if tstate is not the current thread state. */ PyThreadState *current_tstate = _PyThreadState_GET(); if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) { + reentrant = 0; return -1; } @@ -5546,6 +5556,7 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) /* Flag that tracing or profiling is turned on */ tstate->cframe->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); + reentrant = 0; return 0; } @@ -5566,10 +5577,21 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) /* The caller must hold the GIL */ assert(PyGILState_Check()); + static int reentrant = 0; + + if (reentrant) { + _PyErr_SetString(tstate, PyExc_RuntimeError, "Cannot install a trace function " + "while another trace function is being installed"); + reentrant = 0; + return -1; + } + reentrant = 1; + /* Call _PySys_Audit() in the context of the current thread state, even if tstate is not the current thread state. */ PyThreadState *current_tstate = _PyThreadState_GET(); if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) { + reentrant = 0; return -1; } @@ -5579,9 +5601,8 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) tstate->c_traceobj = NULL; /* Must make sure that profiling is not ignored if 'traceobj' is freed */ tstate->cframe->use_tracing = (tstate->c_profilefunc != NULL); - Py_XDECREF(traceobj); - Py_XINCREF(arg); + Py_XDECREF(traceobj); tstate->c_traceobj = arg; tstate->c_tracefunc = func; @@ -5589,6 +5610,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) tstate->cframe->use_tracing = ((func != NULL) || (tstate->c_profilefunc != NULL)); + reentrant = 0; return 0; } |