From a6916d91261db235ee5e14890fef06144b68712b Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 14 Feb 2024 18:21:12 +0100 Subject: [PATCH] [3.11] gh-115243: Fix crash in deque.index() when the deque is concurrently modified (GH-115247) (GH-115466) (cherry picked from commit 671360161f0b7a5ff4c1d062e570962e851b4bde) Co-authored-by: kcatss --- Lib/test/test_deque.py | 6 +++++- Modules/_collectionsmodule.c | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index c0f7138254f3f6..a8eec05f743a1c 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -183,7 +183,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() @@ -195,6 +195,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) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index cc2b90eaa283ea..bc41b8e38b6d22 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1078,7 +1078,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)