summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2015-07-05 00:59:24 (GMT)
committerBenjamin Peterson <benjamin@python.org>2015-07-05 00:59:24 (GMT)
commite54d5321cc527d0c97a95b90b2754f6494e7b3e1 (patch)
tree6798c8787e5648f43ceb868269b56469e1aa8010
parentfdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca (diff)
parent2a48a6eb335e496220aa75f6daae5b56e231b8e4 (diff)
downloadcpython-e54d5321cc527d0c97a95b90b2754f6494e7b3e1.zip
cpython-e54d5321cc527d0c97a95b90b2754f6494e7b3e1.tar.gz
cpython-e54d5321cc527d0c97a95b90b2754f6494e7b3e1.tar.bz2
merge 3.4 (#24407)
-rw-r--r--Lib/test/test_dict.py14
-rw-r--r--Misc/NEWS2
-rw-r--r--Objects/dictobject.c26
3 files changed, 35 insertions, 7 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 9553c66..2488b63 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -937,6 +937,20 @@ class DictTest(unittest.TestCase):
d.popitem()
self.check_reentrant_insertion(mutate)
+ def test_merge_and_mutate(self):
+ class X:
+ def __hash__(self):
+ return 0
+
+ def __eq__(self, o):
+ other.clear()
+ return False
+
+ l = [(i,0) for i in range(1, 1337)]
+ other = dict(l)
+ other[X()] = 0
+ d = {X(): 0, 1: 1}
+ self.assertRaises(RuntimeError, d.update, other)
from test import mapping_tests
diff --git a/Misc/NEWS b/Misc/NEWS
index f440b18..1391c82 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,8 @@ Core and Builtins
- Issue #19235: Add new RecursionError exception. Patch by Georg Brandl.
+- Issue #24407: Fix crash when dict is mutated while being updated.
+
Library
-------
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 3791d5b..5a7de9e 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2039,20 +2039,32 @@ PyDict_Merge(PyObject *a, PyObject *b, int override)
if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
return -1;
for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
- PyObject *value;
+ PyObject *key, *value;
+ Py_hash_t hash;
entry = &other->ma_keys->dk_entries[i];
+ key = entry->me_key;
+ hash = entry->me_hash;
if (other->ma_values)
value = other->ma_values[i];
else
value = entry->me_value;
- if (value != NULL &&
- (override ||
- PyDict_GetItem(a, entry->me_key) == NULL)) {
- if (insertdict(mp, entry->me_key,
- entry->me_hash,
- value) != 0)
+ if (value != NULL) {
+ int err = 0;
+ Py_INCREF(key);
+ Py_INCREF(value);
+ if (override || PyDict_GetItem(a, key) == NULL)
+ err = insertdict(mp, key, hash, value);
+ Py_DECREF(value);
+ Py_DECREF(key);
+ if (err != 0)
+ return -1;
+
+ if (n != DK_SIZE(other->ma_keys)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dict mutated during update");
return -1;
+ }
}
}
}