Skip to content

Commit 3ec9183

Browse files
miss-islingtonkcatss
authored andcommitted
[3.11] pythongh-115243: Fix crash in deque.index() when the deque is concurrently modified (pythonGH-115247) (pythonGH-115466)
(cherry picked from commit 6713601) Co-authored-by: kcatss <[email protected]>
1 parent ebca9c1 commit 3ec9183

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/test/test_deque.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def test_contains(self):
184184
with self.assertRaises(RuntimeError):
185185
n in d
186186

187-
def test_contains_count_stop_crashes(self):
187+
def test_contains_count_index_stop_crashes(self):
188188
class A:
189189
def __eq__(self, other):
190190
d.clear()
@@ -196,6 +196,10 @@ def __eq__(self, other):
196196
with self.assertRaises(RuntimeError):
197197
_ = d.count(3)
198198

199+
d = deque([A()])
200+
with self.assertRaises(RuntimeError):
201+
d.index(0)
202+
199203
def test_extend(self):
200204
d = deque('a')
201205
self.assertRaises(TypeError, d.extend, 1)

Modules/_collectionsmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,9 @@ deque_index(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs)
10771077
while (--n >= 0) {
10781078
CHECK_NOT_END(b);
10791079
item = b->data[index];
1080+
Py_INCREF(item);
10801081
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
1082+
Py_DECREF(item);
10811083
if (cmp > 0)
10821084
return PyLong_FromSsize_t(stop - n - 1);
10831085
if (cmp < 0)

0 commit comments

Comments
 (0)