summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-10-03 21:03:26 -0700
committerGitHub <noreply@github.com>2022-10-03 21:03:26 -0700
commit4591dae421cc1415bcf982cccbf5de4f787df030 (patch)
tree1a0a960a4684c89df3af0ff577a1e37808214a12
parent0e3a046b4849155f0c0890d18d4529a75be6a525 (diff)
downloadpython-4591dae421cc1415bcf982cccbf5de4f787df030.tar.gz
python-4591dae421cc1415bcf982cccbf5de4f787df030.zip
[3.11] GH-97752: Clear the previous member of newly-created generator/coroutine frames (GH-97812)
(cherry picked from commit 93fcc1f4133e177882850177c2c047d46019b812)
-rw-r--r--Lib/test/test_generators.py19
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-10-03-13-35-48.gh-issue-97752.0xTjJY.rst2
-rw-r--r--Python/frame.c3
3 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index e5aa7da1e0d..353073dbfce 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -206,6 +206,25 @@ class GeneratorTest(unittest.TestCase):
finally:
gc.set_threshold(*thresholds)
+ def test_ag_frame_f_back(self):
+ async def f():
+ yield
+ ag = f()
+ self.assertIsNone(ag.ag_frame.f_back)
+
+ def test_cr_frame_f_back(self):
+ async def f():
+ pass
+ cr = f()
+ self.assertIsNone(cr.cr_frame.f_back)
+ cr.close() # Suppress RuntimeWarning.
+
+ def test_gi_frame_f_back(self):
+ def f():
+ yield
+ gi = f()
+ self.assertIsNone(gi.gi_frame.f_back)
+
class ExceptionTest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-03-13-35-48.gh-issue-97752.0xTjJY.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-03-13-35-48.gh-issue-97752.0xTjJY.rst
new file mode 100644
index 00000000000..c6563507034
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-03-13-35-48.gh-issue-97752.0xTjJY.rst
@@ -0,0 +1,2 @@
+Fix possible data corruption or crashes when accessing the ``f_back`` member
+of newly-created generator or coroutine frames.
diff --git a/Python/frame.c b/Python/frame.c
index c4e93492e08..45072da0426 100644
--- a/Python/frame.c
+++ b/Python/frame.c
@@ -52,6 +52,9 @@ _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
assert(src->stacktop >= src->f_code->co_nlocalsplus);
Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src;
memcpy(dest, src, size);
+ // Don't leave a dangling pointer to the old frame when creating generators
+ // and coroutines:
+ dest->previous = NULL;
}