gh-132762: Fix underallocation bug in dict.fromkeys()(gh-133627)

The function `dict_set_fromkeys()` adds elements of a set to an existing
dictionary. The size of the expanded dictionary was estimated with
`PySet_GET_SIZE(iterable)`, which did not take into account the size of the
existing dictionary.
This commit is contained in:
Angela Liss 2025-05-08 13:13:11 -04:00 committed by GitHub
parent 2d82ab761a
commit 421ba589d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View file

@ -3178,9 +3178,10 @@ dict_set_fromkeys(PyInterpreterState *interp, PyDictObject *mp,
Py_ssize_t pos = 0;
PyObject *key;
Py_hash_t hash;
if (dictresize(interp, mp,
estimate_log2_keysize(PySet_GET_SIZE(iterable)), 0)) {
uint8_t new_size = Py_MAX(
estimate_log2_keysize(PySet_GET_SIZE(iterable)),
DK_LOG_SIZE(mp->ma_keys));
if (dictresize(interp, mp, new_size, 0)) {
Py_DECREF(mp);
return NULL;
}