Skip to content

Commit

Permalink
pythongh-125038: PyIter_Checks are added for some FOR_ITER bytecodes
Browse files Browse the repository at this point in the history
SIGSEGV on generators in case of gi_frame.f_locals is fixed.
This applies to _FOR_ITER bytecode.
Similar checks are added to _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations.
  • Loading branch information
efimov-mikhail committed Oct 7, 2024
1 parent da071fa commit ffc0958
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
15 changes: 12 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2804,7 +2804,10 @@ dummy_func(
replaced op(_FOR_ITER, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
PyObject *next_o = NULL;
if (PyIter_Check(iter_o)) {
next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
}
if (next_o == NULL) {
if (_PyErr_Occurred(tstate)) {
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
Expand All @@ -2830,7 +2833,10 @@ dummy_func(
op(_FOR_ITER_TIER_TWO, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
PyObject *next_o = NULL;
if (PyIter_Check(iter_o)) {
next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
}
if (next_o == NULL) {
if (_PyErr_Occurred(tstate)) {
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
Expand All @@ -2854,7 +2860,10 @@ dummy_func(
_Py_CODEUNIT *target;
_PyStackRef iter_stackref = TOP();
PyObject *iter = PyStackRef_AsPyObjectBorrow(iter_stackref);
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
PyObject *next = NULL;
if (PyIter_Check(iter)) {
next = (*Py_TYPE(iter)->tp_iternext)(iter);
}
if (next != NULL) {
PUSH(PyStackRef_FromPyObjectSteal(next));
target = next_instr;
Expand Down
9 changes: 6 additions & 3 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ffc0958

Please sign in to comment.