Skip to content

Commit 93fcc1f

Browse files
authored
GH-97752: Clear the previous member of newly-created generator/coroutine frames (GH-97795)
1 parent d053c47 commit 93fcc1f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/test_generators.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,25 @@ def __del__(self):
206206
finally:
207207
gc.set_threshold(*thresholds)
208208

209+
def test_ag_frame_f_back(self):
210+
async def f():
211+
yield
212+
ag = f()
213+
self.assertIsNone(ag.ag_frame.f_back)
214+
215+
def test_cr_frame_f_back(self):
216+
async def f():
217+
pass
218+
cr = f()
219+
self.assertIsNone(cr.cr_frame.f_back)
220+
cr.close() # Suppress RuntimeWarning.
221+
222+
def test_gi_frame_f_back(self):
223+
def f():
224+
yield
225+
gi = f()
226+
self.assertIsNone(gi.gi_frame.f_back)
227+
209228

210229

211230
class ExceptionTest(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix possible data corruption or crashes when accessing the ``f_back`` member
2+
of newly-created generator or coroutine frames.

Python/frame.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
5454
assert(src->stacktop >= src->f_code->co_nlocalsplus);
5555
Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src;
5656
memcpy(dest, src, size);
57+
// Don't leave a dangling pointer to the old frame when creating generators
58+
// and coroutines:
59+
dest->previous = NULL;
5760
}
5861

5962

0 commit comments

Comments
 (0)