]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39453: Make list.__contains__ hold strong references to avoid crashes (GH-18181)
authorDong-hee Na <donghee.na92@gmail.com>
Mon, 27 Jan 2020 15:02:23 +0000 (00:02 +0900)
committerPablo Galindo <Pablogsal@gmail.com>
Mon, 27 Jan 2020 15:02:23 +0000 (15:02 +0000)
Lib/test/test_list.py
Misc/NEWS.d/next/Core and Builtins/2020-01-25-23-51-17.bpo-39453.xCOkYk.rst [new file with mode: 0644]
Objects/listobject.c

index 6e3c4c109300e618fccabc8a37a914ea253b89c8..33a55f76d9b2ccca4d18d66d6c632ea61f3e7399 100644 (file)
@@ -221,6 +221,11 @@ class ListTest(list_tests.CommonTest):
         with self.assertRaises(ValueError):
             lst.remove(lst)
 
+        # bpo-39453: list.__contains__ was not holding strong references
+        # to list elements while calling PyObject_RichCompareBool().
+        lst = [X(), X()]
+        3 in lst
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-01-25-23-51-17.bpo-39453.xCOkYk.rst b/Misc/NEWS.d/next/Core and Builtins/2020-01-25-23-51-17.bpo-39453.xCOkYk.rst
new file mode 100644 (file)
index 0000000..8c2e49f
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed a possible crash in :meth:`list.__contains__` when a list is changed
+during comparing items. Patch by Dong-hee Na.
index a4e90dbf90cf98c06bc3b2c96776fc38e13eea49..38055d5f5f3410f759a0a723c5ae9f8f186182f8 100644 (file)
@@ -445,11 +445,16 @@ list_length(PyListObject *a)
 static int
 list_contains(PyListObject *a, PyObject *el)
 {
+    PyObject *item;
     Py_ssize_t i;
     int cmp;
 
-    for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
+    for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
+        item = PyList_GET_ITEM(a, i);
+        Py_INCREF(item);
         cmp = PyObject_RichCompareBool(PyList_GET_ITEM(a, i), el, Py_EQ);
+        Py_DECREF(item);
+    }
     return cmp;
 }