diff options
author | Tian Gao <gaogaotiantian@hotmail.com> | 2024-05-08 00:48:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-08 00:48:05 (GMT) |
commit | e7aec8713f84e90b20c9baae6b7a91d87f327ff0 (patch) | |
tree | c6619034e5a7551ba84d3c7db39167785167e850 /Objects | |
parent | 2f0a338be66e276a700f86af51d5ef54a138cbfb (diff) | |
download | cpython-e7aec8713f84e90b20c9baae6b7a91d87f327ff0.zip cpython-e7aec8713f84e90b20c9baae6b7a91d87f327ff0.tar.gz cpython-e7aec8713f84e90b20c9baae6b7a91d87f327ff0.tar.bz2 |
gh-118746: Fix crash in frame_getlocals and _PyFrame_GetLocals (#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.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/frameobject.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 26a04cb..d7fcb19 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -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); } @@ -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); } |