Skip to content

Commit 28b148f

Browse files
pythongh-126220: Fix crash on calls to _lsprof.Profiler methods with 0 args (backportable) (python#126271)
Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 72dd471 commit 28b148f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Lib/test/test_cprofile.py

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ def test_bad_counter_during_dealloc(self):
3030

3131
self.assertEqual(cm.unraisable.exc_type, TypeError)
3232

33+
def test_crash_with_not_enough_args(self):
34+
# gh-126220
35+
import _lsprof
36+
37+
for profile in [_lsprof.Profiler(), cProfile.Profile()]:
38+
for method in [
39+
"_pystart_callback",
40+
"_pyreturn_callback",
41+
"_ccall_callback",
42+
"_creturn_callback",
43+
]:
44+
with self.subTest(profile=profile, method=method):
45+
method_obj = getattr(profile, method)
46+
with self.assertRaises(TypeError):
47+
method_obj() # should not crash
48+
3349
def test_evil_external_timer(self):
3450
# gh-120289
3551
# Disabling profiler in external timer should not crash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash in :class:`!cProfile.Profile` and :class:`!_lsprof.Profiler` when their
2+
callbacks were directly called with 0 arguments.

Modules/_lsprof.c

+24
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@ setBuiltins(ProfilerObject *pObj, int nvalue)
608608

609609
PyObject* pystart_callback(ProfilerObject* self, PyObject *const *args, Py_ssize_t size)
610610
{
611+
if (size < 2) {
612+
PyErr_Format(PyExc_TypeError,
613+
"_pystart_callback expected 2 arguments, got %zd",
614+
size);
615+
return NULL;
616+
}
611617
PyObject* code = args[0];
612618
ptrace_enter_call((PyObject*)self, (void *)code, (PyObject *)code);
613619

@@ -616,6 +622,12 @@ PyObject* pystart_callback(ProfilerObject* self, PyObject *const *args, Py_ssize
616622

617623
PyObject* pyreturn_callback(ProfilerObject* self, PyObject *const *args, Py_ssize_t size)
618624
{
625+
if (size < 3) {
626+
PyErr_Format(PyExc_TypeError,
627+
"_pyreturn_callback expected 3 arguments, got %zd",
628+
size);
629+
return NULL;
630+
}
619631
PyObject* code = args[0];
620632
ptrace_leave_call((PyObject*)self, (void *)code);
621633

@@ -651,6 +663,12 @@ PyObject* get_cfunc_from_callable(PyObject* callable, PyObject* self_arg, PyObje
651663

652664
PyObject* ccall_callback(ProfilerObject* self, PyObject *const *args, Py_ssize_t size)
653665
{
666+
if (size < 4) {
667+
PyErr_Format(PyExc_TypeError,
668+
"_ccall_callback expected 4 arguments, got %zd",
669+
size);
670+
return NULL;
671+
}
654672
if (self->flags & POF_BUILTINS) {
655673
PyObject* callable = args[2];
656674
PyObject* self_arg = args[3];
@@ -669,6 +687,12 @@ PyObject* ccall_callback(ProfilerObject* self, PyObject *const *args, Py_ssize_t
669687

670688
PyObject* creturn_callback(ProfilerObject* self, PyObject *const *args, Py_ssize_t size)
671689
{
690+
if (size < 4) {
691+
PyErr_Format(PyExc_TypeError,
692+
"_creturn_callback expected 4 arguments, got %zd",
693+
size);
694+
return NULL;
695+
}
672696
if (self->flags & POF_BUILTINS) {
673697
PyObject* callable = args[2];
674698
PyObject* self_arg = args[3];

0 commit comments

Comments
 (0)