merge 3.4 (#24044)
diff --git a/Misc/NEWS b/Misc/NEWS
index 974e9ca..4f702d0 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -78,6 +78,9 @@
 - Issue #23466: %c, %o, %x, and %X in bytes formatting now raise TypeError on
   non-integer input.
 
+- Issue #24044: Fix possible null pointer dereference in list.sort in out of
+  memory conditions.
+
 Library
 -------
 
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 8f88d18..45e54ce 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1961,8 +1961,10 @@
             keys = &ms.temparray[saved_ob_size+1];
         else {
             keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
-            if (keys == NULL)
-                return NULL;
+            if (keys == NULL) {
+                PyErr_NoMemory();
+                goto keyfunc_fail;
+            }
         }
 
         for (i = 0; i < saved_ob_size ; i++) {