diff options
author | Benjamin Peterson <benjamin@python.org> | 2015-07-05 00:55:16 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2015-07-05 00:55:16 (GMT) |
commit | a82f77fb00ca3bd3eb27a6a5d77a19eadcf0ba31 (patch) | |
tree | 8512d60d455368264a72505c27fb6164925107dc /Objects | |
parent | dac3ab84c73eb99265f0cf4863897c8e8302dbfd (diff) | |
download | cpython-a82f77fb00ca3bd3eb27a6a5d77a19eadcf0ba31.zip cpython-a82f77fb00ca3bd3eb27a6a5d77a19eadcf0ba31.tar.gz cpython-a82f77fb00ca3bd3eb27a6a5d77a19eadcf0ba31.tar.bz2 |
protect against mutation of the dict during insertion (closes #24407)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 7aa5ea8..953484c 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1941,20 +1941,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; + } } } } |