]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121652: Handle `allocate_weakref` returning NULL (#121653)
authorSam Gross <colesbury@gmail.com>
Sat, 13 Jul 2024 16:07:52 +0000 (12:07 -0400)
committerGitHub <noreply@github.com>
Sat, 13 Jul 2024 16:07:52 +0000 (12:07 -0400)
The `allocate_weakref` may return NULL when out of memory. We need to
handle that case and propagate the error.

Objects/weakrefobject.c

index 0fcd37d949b34abbc78d16d2a5f8ae74e35b78df..61f05514a480231381175d272382d824fc06da7e 100644 (file)
@@ -426,6 +426,10 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
             return basic_ref;
         }
         PyWeakReference *newref = allocate_weakref(type, obj, callback);
+        if (newref == NULL) {
+            UNLOCK_WEAKREFS(obj);
+            return NULL;
+        }
         insert_weakref(newref, list);
         UNLOCK_WEAKREFS(obj);
         return newref;
@@ -433,6 +437,9 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
     else {
         // We may not be able to safely allocate inside the lock
         PyWeakReference *newref = allocate_weakref(type, obj, callback);
+        if (newref == NULL) {
+            return NULL;
+        }
         LOCK_WEAKREFS(obj);
         insert_weakref(newref, list);
         UNLOCK_WEAKREFS(obj);