Skip to content

Commit f785cb5

Browse files
vstinnermiss-islington
authored andcommitted
[3.14] gh-146092: Handle _PyFrame_GetFrameObject() failures properly (GH-146124) (GH-146132)
gh-146092: Handle _PyFrame_GetFrameObject() failures properly (GH-146124) * Fix _PyFrame_GetLocals() and _PyFrame_GetLocals() error handling. * _PyEval_ExceptionGroupMatch() now fails on _PyFrame_GetLocals() error. (cherry picked from commit 8eeb800) Co-authored-by: Victor Stinner <vstinner@python.org> (cherry picked from commit e1e4852)
1 parent c7d7e1c commit f785cb5

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Objects/frameobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,9 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame)
20932093
}
20942094

20952095
PyFrameObject* f = _PyFrame_GetFrameObject(frame);
2096+
if (f == NULL) {
2097+
return NULL;
2098+
}
20962099

20972100
return _PyFrameLocalsProxy_New(f);
20982101
}

Python/ceval.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,14 +2031,17 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
20312031
PyThreadState *tstate = _PyThreadState_GET();
20322032
_PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
20332033
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2034-
if (f != NULL) {
2035-
PyObject *tb = _PyTraceBack_FromFrame(NULL, f);
2036-
if (tb == NULL) {
2037-
return -1;
2038-
}
2039-
PyException_SetTraceback(wrapped, tb);
2040-
Py_DECREF(tb);
2034+
if (f == NULL) {
2035+
Py_DECREF(wrapped);
2036+
return -1;
2037+
}
2038+
2039+
PyObject *tb = _PyTraceBack_FromFrame(NULL, f);
2040+
if (tb == NULL) {
2041+
return -1;
20412042
}
2043+
PyException_SetTraceback(wrapped, tb);
2044+
Py_DECREF(tb);
20422045
*match = wrapped;
20432046
}
20442047
*rest = Py_NewRef(Py_None);
@@ -2505,6 +2508,11 @@ PyEval_GetLocals(void)
25052508

25062509
if (PyFrameLocalsProxy_Check(locals)) {
25072510
PyFrameObject *f = _PyFrame_GetFrameObject(current_frame);
2511+
if (f == NULL) {
2512+
Py_DECREF(locals);
2513+
return NULL;
2514+
}
2515+
25082516
PyObject *ret = f->f_locals_cache;
25092517
if (ret == NULL) {
25102518
ret = PyDict_New();

0 commit comments

Comments
 (0)