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

[3.11] gh-115243: Fix crash in deque.index() when the deque is concurrently modified (GH-115247) #115466

Merged
merged 1 commit into from
Feb 14, 2024
Merged
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
6 changes: 5 additions & 1 deletion Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_contains(self):
with self.assertRaises(RuntimeError):
n in d

def test_contains_count_stop_crashes(self):
def test_contains_count_index_stop_crashes(self):
class A:
def __eq__(self, other):
d.clear()
Expand All @@ -178,6 +178,10 @@ def __eq__(self, other):
with self.assertRaises(RuntimeError):
_ = d.count(3)

d = deque([A()])
with self.assertRaises(RuntimeError):
d.index(0)

def test_extend(self):
d = deque('a')
self.assertRaises(TypeError, d.extend, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible crashes in :meth:`collections.deque.index` when the deque is concurrently modified.
3 changes: 2 additions & 1 deletion Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,8 +1084,9 @@ deque_index(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
n = stop - i;
while (--n >= 0) {
CHECK_NOT_END(b);
item = b->data[index];
item = Py_NewRef(b->data[index]);
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
Py_DECREF(item);
if (cmp > 0)
return PyLong_FromSsize_t(stop - n - 1);
if (cmp < 0)
Expand Down
Loading