Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128198: Add missing error checks for usages of PyIter_Next #128199

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,14 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(item);
}
Py_DECREF(eager_iter);

/* Check if loop ended because of exception in PyIter_Next */
if (PyErr_Occurred()) {
Py_DECREF(tasks);
Py_DECREF(loop);
return NULL;
}

int err = 0;
ASYNCIO_STATE_LOCK(state);
TaskObj *first = &state->asyncio_tasks.first;
Expand Down Expand Up @@ -3662,6 +3670,12 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
}
Py_DECREF(scheduled_iter);
Py_DECREF(loop);

/* Check if loop ended because of exception in PyIter_Next */
if (PyErr_Occurred()) {
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to decref the tasks as well.

}

return tasks;
}

Expand Down
9 changes: 6 additions & 3 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1667,10 +1667,13 @@ deque_richcompare(PyObject *v, PyObject *w, int op)
if (it2 == NULL)
goto done;
for (;;) {
x = PyIter_Next(it1);
if (x == NULL && PyErr_Occurred())
if ((x = PyIter_Next(it1)) == NULL && PyErr_Occurred()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to what was before. Please revert.

goto done;
y = PyIter_Next(it2);
}
if ((y = PyIter_Next(it2)) == NULL && PyErr_Occurred()) {
Py_XDECREF(x);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already handled by lines 1677 + 1689 + 1691. PLease revert.

goto done;
}
if (x == NULL || y == NULL)
break;
b = PyObject_RichCompareBool(x, y, Py_EQ);
Expand Down
5 changes: 5 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ framelocalsproxy_merge(PyObject* self, PyObject* other)

Py_DECREF(iter);

/* Check if loop ended because of exception in PyIter_Next */
if (PyErr_Occurred()) {
return -1;
}

return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions Objects/namespaceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ namespace_repr(PyObject *ns)
goto error;
}

/* Check if loop ended because of exception in PyIter_Next */
if (PyErr_Occurred()) {
goto error;
}

separator = PyUnicode_FromString(", ");
if (separator == NULL)
goto error;
Expand Down
Loading