Skip to content

Commit 583d164

Browse files
committed
Don't expose incomplete frames as frame objects.
1 parent 87e97ae commit 583d164

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Include/internal/pycore_frame.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
210210
return (PyGenObject *)(((char *)frame) - offset_in_gen);
211211
}
212212

213+
214+
static inline bool
215+
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
216+
{
217+
return frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
218+
}
219+
213220
#ifdef __cplusplus
214221
}
215222
#endif

Python/frame.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
6868
f->f_frame = frame;
6969
frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
7070
assert(f->f_back == NULL);
71-
if (frame->previous != NULL) {
71+
_PyInterpreterFrame *prev = frame->previous;
72+
while (prev && _PyFrame_IsIncomplete(prev)) {
73+
prev = prev->previous;
74+
}
75+
if (prev) {
7276
/* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
73-
PyFrameObject *back = _PyFrame_GetFrameObject(frame->previous);
77+
PyFrameObject *back = _PyFrame_GetFrameObject(prev);
7478
if (back == NULL) {
7579
/* Memory error here. */
7680
assert(PyErr_ExceptionMatches(PyExc_MemoryError));

Python/sysmodule.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,9 +1776,17 @@ sys__getframe_impl(PyObject *module, int depth)
17761776
return NULL;
17771777
}
17781778

1779-
while (depth > 0 && frame != NULL) {
1780-
frame = frame->previous;
1781-
--depth;
1779+
if (frame != NULL) {
1780+
while (depth > 0) {
1781+
frame = frame->previous;
1782+
if (frame == NULL) {
1783+
break;
1784+
}
1785+
if (_PyFrame_IsIncomplete(frame)) {
1786+
continue;
1787+
}
1788+
--depth;
1789+
}
17821790
}
17831791
if (frame == NULL) {
17841792
_PyErr_SetString(tstate, PyExc_ValueError,

0 commit comments

Comments
 (0)