From 68ddb59417ee0b0dedf5c8b66a304138c9ce0a63 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 13 Oct 2018 03:25:05 -0600 Subject: [PATCH] [2.7] bpo-22851: Fix a segfault when accessing generator.gi_frame.f_restricted. (GH-9348) Frame's field f_tstate is NULL when the generator is exhausted. --- Include/frameobject.h | 2 +- Lib/test/test_generators.py | 11 +++++++++++ .../2018-09-17-04-41-42.bpo-22851.0hsJPh.rst | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-09-17-04-41-42.bpo-22851.0hsJPh.rst diff --git a/Include/frameobject.h b/Include/frameobject.h index 843908159ddb..34603794c655 100644 --- a/Include/frameobject.h +++ b/Include/frameobject.h @@ -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 *); diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 0f7bf19abb8c..10285f67a2c6 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -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 index 000000000000..c5f524940085 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-09-17-04-41-42.bpo-22851.0hsJPh.rst @@ -0,0 +1,2 @@ +Fix a segfault when accessing ``generator.gi_frame.f_restricted`` when the +generator is exhausted. Patch by Zackery Spytz. -- 2.47.3