]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115243: Fix crash in deque.index() when the deque is concurrently modified (GH...
authorkcatss <kcats9731@gmail.com>
Wed, 14 Feb 2024 16:08:26 +0000 (01:08 +0900)
committerGitHub <noreply@github.com>
Wed, 14 Feb 2024 16:08:26 +0000 (16:08 +0000)
Lib/test/test_deque.py
Misc/NEWS.d/next/Security/2024-02-12-00-33-01.gh-issue-115243.e1oGX8.rst [new file with mode: 0644]
Modules/_collectionsmodule.c

index ae1dfacd7262e404b102bd47a62c8f3bbc483bc1..4679f297fd7f4a134c12552b96671f4a7e4f6b10 100644 (file)
@@ -166,7 +166,7 @@ class TestBasic(unittest.TestCase):
         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()
@@ -178,6 +178,10 @@ class TestBasic(unittest.TestCase):
         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/Misc/NEWS.d/next/Security/2024-02-12-00-33-01.gh-issue-115243.e1oGX8.rst b/Misc/NEWS.d/next/Security/2024-02-12-00-33-01.gh-issue-115243.e1oGX8.rst
new file mode 100644 (file)
index 0000000..ae0e910
--- /dev/null
@@ -0,0 +1 @@
+Fix possible crashes in :meth:`collections.deque.index` when the deque is concurrently modified.
index ef77d34b10e47b96e020781a56f8bf10c3887061..4fa76d62bc3f8d8c4f4978e47c025d40abbd4ca8 100644 (file)
@@ -1218,8 +1218,9 @@ deque_index_impl(dequeobject *deque, PyObject *v, Py_ssize_t start,
     n = stop - i;
     while (--n >= 0) {
         CHECK_NOT_END(b);
-        item = b->data[index];
+        item = Py_NewRef(b->data[index]);
         cmp = PyObject_RichCompareBool(item, v, Py_EQ);
+        Py_DECREF(item);
         if (cmp > 0)
             return PyLong_FromSsize_t(stop - n - 1);
         if (cmp < 0)