Skip to content

Commit a6916d9

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 558b2cb commit a6916d9

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
@@ -183,7 +183,7 @@ def test_contains(self):
183183
with self.assertRaises(RuntimeError):
184184
n in d
185185

186-
def test_contains_count_stop_crashes(self):
186+
def test_contains_count_index_stop_crashes(self):
187187
class A:
188188
def __eq__(self, other):
189189
d.clear()
@@ -195,6 +195,10 @@ def __eq__(self, other):
195195
with self.assertRaises(RuntimeError):
196196
_ = d.count(3)
197197

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

Modules/_collectionsmodule.c

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

0 commit comments

Comments
 (0)