Skip to content

Commit ffc0958

Browse files
pythongh-125038: PyIter_Checks are added for some FOR_ITER bytecodes
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.
1 parent da071fa commit ffc0958

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

Python/bytecodes.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,10 @@ dummy_func(
28042804
replaced op(_FOR_ITER, (iter -- iter, next)) {
28052805
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
28062806
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
2807-
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
2807+
PyObject *next_o = NULL;
2808+
if (PyIter_Check(iter_o)) {
2809+
next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
2810+
}
28082811
if (next_o == NULL) {
28092812
if (_PyErr_Occurred(tstate)) {
28102813
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
@@ -2830,7 +2833,10 @@ dummy_func(
28302833
op(_FOR_ITER_TIER_TWO, (iter -- iter, next)) {
28312834
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
28322835
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
2833-
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
2836+
PyObject *next_o = NULL;
2837+
if (PyIter_Check(iter_o)) {
2838+
next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
2839+
}
28342840
if (next_o == NULL) {
28352841
if (_PyErr_Occurred(tstate)) {
28362842
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
@@ -2854,7 +2860,10 @@ dummy_func(
28542860
_Py_CODEUNIT *target;
28552861
_PyStackRef iter_stackref = TOP();
28562862
PyObject *iter = PyStackRef_AsPyObjectBorrow(iter_stackref);
2857-
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
2863+
PyObject *next = NULL;
2864+
if (PyIter_Check(iter)) {
2865+
next = (*Py_TYPE(iter)->tp_iternext)(iter);
2866+
}
28582867
if (next != NULL) {
28592868
PUSH(PyStackRef_FromPyObjectSteal(next));
28602869
target = next_instr;

Python/executor_cases.c.h

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)