Skip to content

Commit c4cf745

Browse files
GH-96754: Check whether the interpreter frame is complete before creating frame object. (GH-96776) (#96787)
(cherry picked from commit 12c5f32) Co-authored-by: Mark Shannon <mark@hotpy.org> Co-authored-by: Mark Shannon <mark@hotpy.org>
1 parent 8238fa9 commit c4cf745

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make sure that all frame objects created are created from valid interpreter
2+
frames. Prevents the possibility of invalid frames in backtraces and signal
3+
handlers.

Modules/signalmodule.c

+3
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,9 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
18321832
_Py_atomic_store(&is_tripped, 0);
18331833

18341834
_PyInterpreterFrame *frame = tstate->cframe->current_frame;
1835+
while (frame && _PyFrame_IsIncomplete(frame)) {
1836+
frame = frame->previous;
1837+
}
18351838
signal_state_t *state = &signal_global_state;
18361839
for (int i = 1; i < Py_NSIG; i++) {
18371840
if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {

Python/ceval.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -5747,9 +5747,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
57475747
#endif
57485748

57495749
/* Log traceback info. */
5750-
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
5751-
if (f != NULL) {
5752-
PyTraceBack_Here(f);
5750+
if (!_PyFrame_IsIncomplete(frame)) {
5751+
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
5752+
if (f != NULL) {
5753+
PyTraceBack_Here(f);
5754+
}
57535755
}
57545756

57555757
if (tstate->c_tracefunc != NULL) {

Python/pystate.c

+3
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,9 @@ _PyThread_CurrentFrames(void)
13911391
PyThreadState *t;
13921392
for (t = i->threads.head; t != NULL; t = t->next) {
13931393
_PyInterpreterFrame *frame = t->cframe->current_frame;
1394+
while (frame && _PyFrame_IsIncomplete(frame)) {
1395+
frame = frame->previous;
1396+
}
13941397
if (frame == NULL) {
13951398
continue;
13961399
}

0 commit comments

Comments
 (0)