]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-151842: Fix crash upon OOM in `_interpreters.capture_exception` (GH-151843...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 23 Jun 2026 20:58:10 +0000 (22:58 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Jun 2026 20:58:10 +0000 (20:58 +0000)
(cherry picked from commit 5e0747db2f5a063657a117e88b58a20624c848d1)

Co-authored-by: Amrutha <amruthamodela06@gmail.com>
Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst [new file with mode: 0644]
Modules/_interpretersmodule.c
Python/crossinterp.c

diff --git a/Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst b/Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst
new file mode 100644 (file)
index 0000000..8a2ecf3
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash in :func:`!_interpreters.capture_exception` when
+:exc:`MemoryError` happens. Patch by Amrutha Modela.
index e7a91ced48f17601f1281b659b03cd9445e7f2f2..d024dee906ded36192d1296db314a214b91bd310 100644 (file)
@@ -1541,7 +1541,9 @@ _interpreters_capture_exception_impl(PyObject *module, PyObject *exc_arg)
     }
 
 finally:
-    _PyXI_FreeExcInfo(info);
+    if (info != NULL) {
+        _PyXI_FreeExcInfo(info);
+    }
     if (exc != exc_arg) {
         if (PyErr_Occurred()) {
             PyErr_SetRaisedException(exc);
index 6b489bf03f86ecdbbd70d1f5f3a37c7773452e0c..ed77c1be646e275c3f43cb0048eeecf6f8db327b 100644 (file)
@@ -1689,6 +1689,7 @@ _PyXI_NewExcInfo(PyObject *exc)
     }
     _PyXI_excinfo *info = PyMem_RawCalloc(1, sizeof(_PyXI_excinfo));
     if (info == NULL) {
+        PyErr_NoMemory();
         return NULL;
     }
     const char *failure;
@@ -1709,6 +1710,7 @@ _PyXI_NewExcInfo(PyObject *exc)
 void
 _PyXI_FreeExcInfo(_PyXI_excinfo *info)
 {
+    assert(info != NULL);
     _PyXI_excinfo_clear(info);
     PyMem_RawFree(info);
 }