Skip to content

Commit

Permalink
[3.11] pythongh-115243: Fix crash in deque.index() when the deque is …
Browse files Browse the repository at this point in the history
…concurrently modified (pythonGH-115247) (pythonGH-115466)

(cherry picked from commit 6713601)

Co-authored-by: kcatss <kcats9731@gmail.com>
  • Loading branch information
2 people authored and mgorny committed Apr 10, 2024
1 parent ebca9c1 commit 3ec9183
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,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 @@ -196,6 +196,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
2 changes: 2 additions & 0 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,9 @@ deque_index(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
while (--n >= 0) {
CHECK_NOT_END(b);
item = b->data[index];
Py_INCREF(item);
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

0 comments on commit 3ec9183

Please sign in to comment.