Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ class C:
self.clear_traceback_frames(exc.__traceback__)
self.assertIs(None, wr())

@support.cpython_only
def test_frame_clear_during_load_name(self):
def get_frame():
try:
raise RuntimeError
except RuntimeError as e:
return e.__traceback__.tb_frame

frame = get_frame()

class Fuse(dict):
def __getitem__(self, key):
if key == "boom":
frame.clear()
raise KeyError(key)

with self.assertRaises(NameError):
exec("boom", {}, Fuse())


class FrameAttrsTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in ceval where the load name has use-after-free bug.
10 changes: 9 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4061,9 +4061,17 @@ _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *na
"no locals found");
return NULL;
}
if (PyMapping_GetOptionalItem(frame->f_locals, name, &value) < 0) {

PyObject *locals = frame->f_locals;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the same also happen to f_globals? f_builtins?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the same also happen to f_globals? f_builtins?

yes

Py_INCREF(locals);

if (PyMapping_GetOptionalItem(locals, name, &value) < 0) {
Py_DECREF(locals);
return NULL;
}

Py_DECREF(locals);

if (value != NULL) {
return value;
}
Expand Down
Loading