@@ -873,6 +873,7 @@ frame_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
873873 if (globals == NULL )
874874 return NULL ;
875875 f = PyFrame_New (ts , (PyCodeObject * ) f_code , globals , globals );
876+ assert (f -> f_execute == NULL ); /* frame is not executable */
876877 if (f != NULL )
877878 Py_TYPE (f ) = & wrap_PyFrame_Type ;
878879 Py_DECREF (globals );
@@ -883,7 +884,7 @@ frame_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
883884static PyObject *
884885frame_setstate (PyFrameObject * f , PyObject * args )
885886{
886- int /*f_restricted, */ f_lasti , f_lineno , i ;
887+ int f_lasti , f_lineno , i ;
887888 PyObject * f_globals , * f_locals , * blockstack_as_tuple ;
888889 PyObject * localsplus_as_tuple , * exc_as_tuple , * trace , * f_code ;
889890 PyObject * exec_name = NULL ;
@@ -893,6 +894,9 @@ frame_setstate(PyFrameObject *f, PyObject *args)
893894
894895 if (is_wrong_type (Py_TYPE (f ))) return NULL ;
895896
897+ Py_CLEAR (f -> f_globals );
898+ Py_CLEAR (f -> f_locals );
899+
896900 if (!PyArg_ParseTuple (args , frametuplesetstatefmt ,
897901 & PyCode_Type , & f_code ,
898902 & valid ,
@@ -901,7 +905,6 @@ frame_setstate(PyFrameObject *f, PyObject *args)
901905 & have_locals ,
902906 & PyDict_Type , & f_locals ,
903907 & trace ,
904- /* &f_restricted, */
905908 & exc_as_tuple ,
906909 & f_lasti ,
907910 & f_lineno ,
@@ -936,17 +939,21 @@ frame_setstate(PyFrameObject *f, PyObject *args)
936939 goto err_exit ;
937940 }
938941 Py_INCREF (trace );
942+ assert (f -> f_trace == NULL );
939943 f -> f_trace = trace ;
940944 }
941945
942- /* f->f_restricted = f_restricted; */
943-
944946 if (exc_as_tuple != Py_None ) {
945947 if (PyTuple_GET_SIZE (exc_as_tuple ) != 4 ) {
946948 PyErr_SetString (PyExc_ValueError ,
947949 "bad exception tuple for frame" );
948950 goto err_exit ;
949951 }
952+ assert (f -> f_exc_type == NULL );
953+ assert (f -> f_exc_value == NULL );
954+ assert (f -> f_exc_traceback == NULL );
955+ assert (((& f -> f_exc_type ) + 1 == & f -> f_exc_value ) &&
956+ ((& f -> f_exc_type ) + 2 == & f -> f_exc_traceback ));
950957 slp_from_tuple_with_nulls (& f -> f_exc_type , exc_as_tuple );
951958 }
952959
@@ -1022,14 +1029,18 @@ frame_setstate(PyFrameObject *f, PyObject *args)
10221029 }
10231030 }
10241031
1025- /* see if this frame is valid to be run */
1032+ /* See if this frame is valid to be run. */
10261033 f -> f_execute = valid ? good_func : bad_func ;
10271034
10281035 Py_TYPE (f ) = & PyFrame_Type ;
10291036 Py_INCREF (f );
10301037 return (PyObject * ) f ;
10311038err_exit :
1032- Py_XDECREF (f );
1039+ /* Make sure that the frame is not executable. */
1040+ f -> f_execute = NULL ;
1041+ /* Clear members that could leak. */
1042+ PyFrame_Type .tp_clear ((PyObject * )f );
1043+
10331044 return NULL ;
10341045}
10351046
0 commit comments