]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-22851: Fix a segfault when accessing generator.gi_frame.f_restricted. ...
authorZackery Spytz <zspytz@gmail.com>
Sat, 13 Oct 2018 09:25:05 +0000 (03:25 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 13 Oct 2018 09:25:05 +0000 (12:25 +0300)
Frame's field f_tstate is NULL when the generator is exhausted.

Include/frameobject.h
Lib/test/test_generators.py
Misc/NEWS.d/next/Core and Builtins/2018-09-17-04-41-42.bpo-22851.0hsJPh.rst [new file with mode: 0644]

index 843908159ddb8f2241673ce5d35591e515550baa..34603794c655a715a06308e30d8e4f66b0248c26 100644 (file)
@@ -56,7 +56,7 @@ PyAPI_DATA(PyTypeObject) PyFrame_Type;
 
 #define PyFrame_Check(op) (Py_TYPE(op) == &PyFrame_Type)
 #define PyFrame_IsRestricted(f) \
-       ((f)->f_builtins != (f)->f_tstate->interp->builtins)
+       ((f)->f_tstate && (f)->f_builtins != (f)->f_tstate->interp->builtins)
 
 PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
                                        PyObject *, PyObject *);
index 0f7bf19abb8caa5d48e2db7e596d8a8601d7b2f5..10285f67a2c6740af0a756d20a16f56cffc1aba0 100644 (file)
@@ -1877,6 +1877,16 @@ test_generators just happened to be the test that drew these out.
 
 """
 
+crash_test = """
+>>> def foo(): yield
+>>> gen = foo()
+>>> gen.next()
+>>> print gen.gi_frame.f_restricted  # This would segfault.
+False
+
+"""
+
+
 __test__ = {"tut":      tutorial_tests,
             "pep":      pep_tests,
             "email":    email_tests,
@@ -1886,6 +1896,7 @@ __test__ = {"tut":      tutorial_tests,
             "weakref":  weakref_tests,
             "coroutine":  coroutine_tests,
             "refleaks": refleaks_tests,
+            "crash": crash_test,
             }
 
 # Magic test name that regrtest.py invokes *after* importing this module.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-09-17-04-41-42.bpo-22851.0hsJPh.rst b/Misc/NEWS.d/next/Core and Builtins/2018-09-17-04-41-42.bpo-22851.0hsJPh.rst
new file mode 100644 (file)
index 0000000..c5f5249
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a segfault when accessing ``generator.gi_frame.f_restricted`` when the
+generator is exhausted.  Patch by Zackery Spytz.