Skip to content

Commit 6165d55

Browse files
committed
Issue #28147: Fix a memory leak in split-table dictionaries
setattr() must not convert combined table into split table.
1 parent 270a21f commit 6165d55

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

Lib/test/test_dict.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,24 @@ class MyDict(dict):
836836
pass
837837
self._tracked(MyDict())
838838

839+
@support.cpython_only
840+
def test_splittable_setattr_after_pop(self):
841+
"""setattr must not convert combined table into split table"""
842+
# Issue 28147
843+
import _testcapi
844+
845+
class C:
846+
pass
847+
a = C()
848+
a.a = 2
849+
self.assertTrue(_testcapi.dict_hassplittable(a.__dict__))
850+
# dict.popitem() convert it to combined table
851+
a.__dict__.popitem()
852+
self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
853+
# But C should not convert a.__dict__ to split table again.
854+
a.a = 3
855+
self.assertFalse(_testcapi.dict_hassplittable(a.__dict__))
856+
839857
def test_iterator_pickling(self):
840858
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
841859
data = {1:"a", 2:"b", 3:"c"}

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28147: Fix a memory leak in split-table dictionaries: setattr()
14+
must not convert combined table into split table.
15+
1316
- Issue #25677: Correct the positioning of the syntax error caret for
1417
indented blocks. Based on patch by Michael Layzell.
1518

Modules/_testcapimodule.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ test_dict_iteration(PyObject* self)
249249
}
250250

251251

252+
static PyObject*
253+
dict_hassplittable(PyObject *self, PyObject *arg)
254+
{
255+
if (!PyArg_Parse(arg, "O!:dict_hassplittable", &PyDict_Type, &arg)) {
256+
return NULL;
257+
}
258+
return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg));
259+
}
260+
252261
/* Issue #4701: Check that PyObject_Hash implicitly calls
253262
* PyType_Ready if it hasn't already been called
254263
*/
@@ -3858,6 +3867,7 @@ static PyMethodDef TestMethods[] = {
38583867
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
38593868
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
38603869
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
3870+
{"dict_hassplittable", dict_hassplittable, METH_O},
38613871
{"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
38623872
{"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
38633873
{"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS},

Objects/dictobject.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,10 @@ make_keys_shared(PyObject *op)
985985
return NULL;
986986
}
987987
else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
988-
/* Remove dummy keys */
989-
if (dictresize(mp, DK_SIZE(mp->ma_keys)))
988+
/* Remove dummy keys
989+
* -1 is required since dictresize() uses key size > minused
990+
*/
991+
if (dictresize(mp, DK_SIZE(mp->ma_keys) - 1))
990992
return NULL;
991993
}
992994
assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
@@ -2473,7 +2475,8 @@ dict_popitem(PyDictObject *mp)
24732475
}
24742476
/* Convert split table to combined table */
24752477
if (mp->ma_keys->dk_lookup == lookdict_split) {
2476-
if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2478+
/* -1 is required since dictresize() uses key size > minused */
2479+
if (dictresize(mp, DK_SIZE(mp->ma_keys) - 1)) {
24772480
Py_DECREF(res);
24782481
return NULL;
24792482
}
@@ -3848,10 +3851,16 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
38483851
CACHED_KEYS(tp) = NULL;
38493852
DK_DECREF(cached);
38503853
}
3851-
} else {
3854+
}
3855+
else {
3856+
int was_shared = cached == ((PyDictObject *)dict)->ma_keys;
38523857
res = PyDict_SetItem(dict, key, value);
3853-
if (cached != ((PyDictObject *)dict)->ma_keys) {
3854-
/* Either update tp->ht_cached_keys or delete it */
3858+
/* PyDict_SetItem() may call dictresize() and convert split table
3859+
* into combined table. In such case, convert it to split
3860+
* table again and update type's shared key only when this is
3861+
* the only dict sharing key with the type.
3862+
*/
3863+
if (was_shared && cached != ((PyDictObject *)dict)->ma_keys) {
38553864
if (cached->dk_refcnt == 1) {
38563865
CACHED_KEYS(tp) = make_keys_shared(dict);
38573866
} else {

0 commit comments

Comments
 (0)