diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-10-07 00:36:39 (GMT) |
---|---|---|
committer | Pablo Galindo <pablogsal@gmail.com> | 2022-10-22 19:09:08 (GMT) |
commit | d0ab10f6f0163c397af4412175fe9ca0b2d96b4b (patch) | |
tree | 28d41b1a7ff0d6e330be1a961a9e597b18099b71 /Python/frame.c | |
parent | 154b3cd751642b6f21a090ad548785bfa1cbc686 (diff) | |
download | cpython-d0ab10f6f0163c397af4412175fe9ca0b2d96b4b.zip cpython-d0ab10f6f0163c397af4412175fe9ca0b2d96b4b.tar.gz cpython-d0ab10f6f0163c397af4412175fe9ca0b2d96b4b.tar.bz2 |
[3.11] GH-97002: Prevent _PyInterpreterFrames from backing more than one PyFrameObject (GH-98002)
(cherry picked from commit 21a2d9ff550977f2668e2cf1cc15793bf27fa109)
Diffstat (limited to 'Python/frame.c')
-rw-r--r-- | Python/frame.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/Python/frame.c b/Python/frame.c index c4e9349..ef88481 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -35,14 +35,31 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame) Py_XDECREF(error_type); Py_XDECREF(error_value); Py_XDECREF(error_traceback); + return NULL; } - else { - assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); - assert(frame->owner != FRAME_CLEARED); - f->f_frame = frame; - frame->frame_obj = f; - PyErr_Restore(error_type, error_value, error_traceback); + PyErr_Restore(error_type, error_value, error_traceback); + if (frame->frame_obj) { + // GH-97002: How did we get into this horrible situation? Most likely, + // allocating f triggered a GC collection, which ran some code that + // *also* created the same frame... while we were in the middle of + // creating it! See test_sneaky_frame_object in test_frame.py for a + // concrete example. + // + // Regardless, just throw f away and use that frame instead, since it's + // already been exposed to user code. It's actually a bit tricky to do + // this, since we aren't backed by a real _PyInterpreterFrame anymore. + // Just pretend that we have an owned, cleared frame so frame_dealloc + // doesn't make the situation worse: + f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data; + f->f_frame->owner = FRAME_CLEARED; + f->f_frame->frame_obj = f; + Py_DECREF(f); + return frame->frame_obj; } + assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); + assert(frame->owner != FRAME_CLEARED); + f->f_frame = frame; + frame->frame_obj = f; return f; } |