Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 585c95d

Browse files
miss-islingtonpablogsal
authored andcommitted
[3.11] GH-97752: Clear the previous member of newly-created generator/coroutine frames (GH-97812)
(cherry picked from commit 93fcc1f)
1 parent 4e0fda5 commit 585c95d

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/test_generators.py

+19
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):
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

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
6969
assert(src->stacktop >= src->f_code->co_nlocalsplus);
7070
Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src;
7171
memcpy(dest, src, size);
72+
// Don't leave a dangling pointer to the old frame when creating generators
73+
// and coroutines:
74+
dest->previous = NULL;
7275
}
7376

7477

0 commit comments

Comments
 (0)