Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def test_free_after_iterating(self):
support.check_free_after_iterating(self, lambda d: iter(d.items()), dict)

def test_equal_operator_modifying_operand(self):
# test fix for seg fault reported in issue 27945 part 3.
# test fix for seg fault reported in bpo-27945 part 3.
class X():
def __del__(self):
dict_b.clear()
Expand All @@ -1108,6 +1108,16 @@ def __hash__(self):
dict_b = {X(): X()}
self.assertTrue(dict_a == dict_b)

# test fix for seg fault reported in bpo-38588 part 1.
class Y:
def __eq__(self, other):
dict_d.clear()
return True

dict_c = {0: Y()}
dict_d = {0: set()}
self.assertTrue(dict_c == dict_d)

def test_fromkeys_operator_modifying_dict_operand(self):
# test fix for seg fault reported in issue 27945 part 4a.
class X(int):
Expand Down
58 changes: 58 additions & 0 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,63 @@ class L(list): pass
with self.assertRaises(TypeError):
(3,) + L([1,2])

def test_equal_operator_modifying_operand(self):
# test fix for seg fault reported in bpo-38588 part 2.
class X:
def __eq__(self,other) :
list2.clear()
return NotImplemented

class Y:
def __eq__(self, other):
list1.clear()
return NotImplemented

class Z:
def __eq__(self, other):
list3.clear()
return NotImplemented

list1 = [X()]
list2 = [Y()]
self.assertTrue(list1 == list2)

list3 = [Z()]
list4 = [1]
self.assertFalse(list3 == list4)

def test_count_index_remove_crashes(self):
# bpo-38610: The count(), index(), and remove() methods were not
# holding strong references to list elements while calling
# PyObject_RichCompareBool().
class X:
def __eq__(self, other):
lst.clear()
return NotImplemented

lst = [X()]
with self.assertRaises(ValueError):
lst.index(lst)

class L(list):
def __eq__(self, other):
str(other)
return NotImplemented

lst = L([X()])
lst.count(lst)

lst = L([X()])
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
lst = [X(), X()]
X() in lst


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix possible crashes in several list methods by holding strong references to
list elements when calling :c:func:`PyObject_RichCompareBool`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix possible crashes in dict and list when calling
:c:func:`PyObject_RichCompareBool`.
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2757,9 +2757,11 @@ dict_equal(PyDictObject *a, PyDictObject *b)
return -1;
return 0;
}
Py_INCREF(bval);
cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Py_DECREF(key);
Py_DECREF(aval);
Py_DECREF(bval);
if (cmp <= 0) /* error or not equal */
return cmp;
}
Expand Down
35 changes: 29 additions & 6 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,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)
cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Py_EQ);
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
item = PyList_GET_ITEM(a, i);
Py_INCREF(item);
cmp = PyObject_RichCompareBool(el, item, Py_EQ);
Py_DECREF(item);
}
return cmp;
}

Expand Down Expand Up @@ -2164,7 +2168,10 @@ listindex(PyListObject *self, PyObject *args)
stop = 0;
}
for (i = start; i < stop && i < Py_SIZE(self); i++) {
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
PyObject *obj = self->ob_item[i];
Py_INCREF(obj);
int cmp = PyObject_RichCompareBool(obj, v, Py_EQ);
Py_DECREF(obj);
if (cmp > 0)
return PyLong_FromSsize_t(i);
else if (cmp < 0)
Expand All @@ -2181,7 +2188,10 @@ listcount(PyListObject *self, PyObject *v)
Py_ssize_t i;

for (i = 0; i < Py_SIZE(self); i++) {
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
PyObject *obj = self->ob_item[i];
Py_INCREF(obj);
int cmp = PyObject_RichCompareBool(obj, v, Py_EQ);
Py_DECREF(obj);
if (cmp > 0)
count++;
else if (cmp < 0)
Expand All @@ -2196,7 +2206,10 @@ listremove(PyListObject *self, PyObject *v)
Py_ssize_t i;

for (i = 0; i < Py_SIZE(self); i++) {
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
PyObject *obj = self->ob_item[i];
Py_INCREF(obj);
int cmp = PyObject_RichCompareBool(obj, v, Py_EQ);
Py_DECREF(obj);
if (cmp > 0) {
if (list_ass_slice(self, i, i+1,
(PyObject *)NULL) == 0)
Expand Down Expand Up @@ -2245,8 +2258,18 @@ list_richcompare(PyObject *v, PyObject *w, int op)

/* Search for the first index where items are different */
for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
PyObject *vitem = vl->ob_item[i];
PyObject *witem = wl->ob_item[i];
if (vitem == witem) {
continue;
}

Py_INCREF(vitem);
Py_INCREF(witem);
int k = PyObject_RichCompareBool(vl->ob_item[i],
wl->ob_item[i], Py_EQ);
Py_DECREF(vitem);
Py_DECREF(witem);
if (k < 0)
return NULL;
if (!k)
Expand Down