Skip to content

Commit a640a60

Browse files
authored
gh-121652: Handle allocate_weakref returning NULL (#121653)
The `allocate_weakref` may return NULL when out of memory. We need to handle that case and propagate the error.
1 parent a183474 commit a640a60

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Objects/weakrefobject.c

+7
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,20 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
426426
return basic_ref;
427427
}
428428
PyWeakReference *newref = allocate_weakref(type, obj, callback);
429+
if (newref == NULL) {
430+
UNLOCK_WEAKREFS(obj);
431+
return NULL;
432+
}
429433
insert_weakref(newref, list);
430434
UNLOCK_WEAKREFS(obj);
431435
return newref;
432436
}
433437
else {
434438
// We may not be able to safely allocate inside the lock
435439
PyWeakReference *newref = allocate_weakref(type, obj, callback);
440+
if (newref == NULL) {
441+
return NULL;
442+
}
436443
LOCK_WEAKREFS(obj);
437444
insert_weakref(newref, list);
438445
UNLOCK_WEAKREFS(obj);

0 commit comments

Comments
 (0)