From 3ec91830aa08eb222824e12f2ffd5419f9769a8f 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 f913c76ff267de..19133dc12c97d8 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -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() @@ -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) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 2e19b83dcec6fe..ab1ca546521db4 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -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)