Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56868f9

Browse files
jdemeyerserhiy-storchaka
authored andcommitted
bpo-34126: Fix crashes while profiling invalid calls. (GH-8300)
1 parent a692efe commit 56868f9

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

Lib/test/test_sys_setprofile.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,22 @@ def j(p):
334334
(1, 'return', j_ident),
335335
])
336336

337+
# Test an invalid call (bpo-34126)
338+
def test_unbound_method_no_args(self):
339+
def f(p):
340+
dict.get()
341+
f_ident = ident(f)
342+
self.check_events(f, [(1, 'call', f_ident),
343+
(1, 'return', f_ident)])
344+
345+
# Test an invalid call (bpo-34126)
346+
def test_unbound_method_invalid_args(self):
347+
def f(p):
348+
dict.get(print, 42)
349+
f_ident = ident(f)
350+
self.check_events(f, [(1, 'call', f_ident),
351+
(1, 'return', f_ident)])
352+
337353

338354
def ident(function):
339355
if hasattr(function, "f_code"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crashes when profiling certain invalid calls of unbound methods.
2+
Patch by Jeroen Demeyer.

Python/ceval.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4566,10 +4566,16 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
45664566
}
45674567
else if (Py_TYPE(func) == &PyMethodDescr_Type) {
45684568
PyThreadState *tstate = PyThreadState_GET();
4569-
if (tstate->use_tracing && tstate->c_profilefunc) {
4570-
// We need to create PyCFunctionObject for tracing.
4571-
PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4572-
func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4569+
if (nargs > 0 && tstate->use_tracing) {
4570+
/* We need to create a temporary bound method as argument
4571+
for profiling.
4572+
4573+
If nargs == 0, then this cannot work because we have no
4574+
"self". In any case, the call itself would raise
4575+
TypeError (foo needs an argument), so we just skip
4576+
profiling. */
4577+
PyObject *self = stack[0];
4578+
func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
45734579
if (func == NULL) {
45744580
return NULL;
45754581
}

0 commit comments

Comments
 (0)