Skip to content

Commit dab738d

Browse files
committed
gh-153182: Explicitly return NULL when new_keys_object call fails in _PyDict_NewKeysForClass to prevent
a segfault when subsequent code tries to use the returned object
1 parent 8bee2a3 commit dab738d

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash when defining a class in low memory conditions.

Objects/dictobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7219,13 +7219,12 @@ _PyDict_NewKeysForClass(PyHeapTypeObject *cls)
72197219
PyDictKeysObject *keys = new_keys_object(NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1);
72207220
if (keys == NULL) {
72217221
PyErr_Clear();
7222+
return NULL;
72227223
}
7223-
else {
7224-
assert(keys->dk_nentries == 0);
7225-
/* Set to max size+1 as it will shrink by one before each new object */
7226-
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
7227-
keys->dk_kind = DICT_KEYS_SPLIT;
7228-
}
7224+
assert(keys->dk_nentries == 0);
7225+
/* Set to max size+1 as it will shrink by one before each new object */
7226+
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
7227+
keys->dk_kind = DICT_KEYS_SPLIT;
72297228
if (cls->ht_type.tp_dict) {
72307229
PyObject *attrs = PyDict_GetItem(cls->ht_type.tp_dict, &_Py_ID(__static_attributes__));
72317230
if (attrs != NULL && PyTuple_Check(attrs)) {

0 commit comments

Comments
 (0)