Skip to content

[3.7] bpo-38588: Fix possible crashes in dict and list when calling P… #17765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 31, 2019
Merged
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 @@ -1138,7 +1138,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 @@ -1154,6 +1154,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
25 changes: 25 additions & 0 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ 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
Expand Down
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`.
2 changes: 2 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2677,9 +2677,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
10 changes: 10 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2615,8 +2615,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