Skip to content

Commit

Permalink
pythongh-118746: Fix crash in frame_getlocals and _PyFrame_GetLocals (p…
Browse files Browse the repository at this point in the history
…ython#118748)

We don't know how to create an unoptimized frame with f_locals == NULL,
but they are seen in the wild, and this fixes the crash.
  • Loading branch information
gaogaotiantian authored May 8, 2024
1 parent 2f0a338 commit e7aec87
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,15 @@ frame_getlocals(PyFrameObject *f, void *closure)
PyCodeObject *co = _PyFrame_GetCode(f->f_frame);

if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(f->f_frame)) {
if (f->f_frame->f_locals == NULL) {
// We found cases when f_locals is NULL for non-optimized code.
// We fill the f_locals with an empty dict to avoid crash until
// we find the root cause.
f->f_frame->f_locals = PyDict_New();
if (f->f_frame->f_locals == NULL) {
return NULL;
}
}
return Py_NewRef(f->f_frame->f_locals);
}

Expand Down Expand Up @@ -1937,6 +1946,15 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame)
PyCodeObject *co = _PyFrame_GetCode(frame);

if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(frame)) {
if (frame->f_locals == NULL) {
// We found cases when f_locals is NULL for non-optimized code.
// We fill the f_locals with an empty dict to avoid crash until
// we find the root cause.
frame->f_locals = PyDict_New();
if (frame->f_locals == NULL) {
return NULL;
}
}
return Py_NewRef(frame->f_locals);
}

Expand Down

0 comments on commit e7aec87

Please sign in to comment.