mirror of
https://github.com/python/cpython.git
synced 2025-10-10 08:53:14 +00:00
bpo-39453: Make list.__contains__ hold strong references to avoid crashes (GH-18181)
This commit is contained in:
parent
a46575a8f2
commit
4dbf2d8c67
3 changed files with 13 additions and 1 deletions
|
@ -221,6 +221,11 @@ class ListTest(list_tests.CommonTest):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
lst.remove(lst)
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -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.
|
|
@ -445,11 +445,16 @@ list_length(PyListObject *a)
|
||||||
static int
|
static int
|
||||||
list_contains(PyListObject *a, PyObject *el)
|
list_contains(PyListObject *a, PyObject *el)
|
||||||
{
|
{
|
||||||
|
PyObject *item;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
int cmp;
|
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);
|
cmp = PyObject_RichCompareBool(PyList_GET_ITEM(a, i), el, Py_EQ);
|
||||||
|
Py_DECREF(item);
|
||||||
|
}
|
||||||
return cmp;
|
return cmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue