Skip to content

Commit f78e1aa

Browse files
[3.13] gh-121652: Handle allocate_weakref returning NULL (GH-121653) (#121721)
The `allocate_weakref` may return NULL when out of memory. We need to handle that case and propagate the error. (cherry picked from commit a640a60) Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent f0c29a2 commit f78e1aa

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)