Skip to content

Commit 415e218

Browse files
[3.15] gh-151842: Fix crash upon OOM in _interpreters.capture_exception (GH-151843) (GH-152031)
(cherry picked from commit 5e0747d) Co-authored-by: Amrutha <amruthamodela06@gmail.com>
1 parent bd39eea commit 415e218

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :func:`!_interpreters.capture_exception` when
2+
:exc:`MemoryError` happens. Patch by Amrutha Modela.

Modules/_interpretersmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,9 @@ _interpreters_capture_exception_impl(PyObject *module, PyObject *exc_arg)
15411541
}
15421542

15431543
finally:
1544-
_PyXI_FreeExcInfo(info);
1544+
if (info != NULL) {
1545+
_PyXI_FreeExcInfo(info);
1546+
}
15451547
if (exc != exc_arg) {
15461548
if (PyErr_Occurred()) {
15471549
PyErr_SetRaisedException(exc);

Python/crossinterp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,7 @@ _PyXI_NewExcInfo(PyObject *exc)
16891689
}
16901690
_PyXI_excinfo *info = PyMem_RawCalloc(1, sizeof(_PyXI_excinfo));
16911691
if (info == NULL) {
1692+
PyErr_NoMemory();
16921693
return NULL;
16931694
}
16941695
const char *failure;
@@ -1709,6 +1710,7 @@ _PyXI_NewExcInfo(PyObject *exc)
17091710
void
17101711
_PyXI_FreeExcInfo(_PyXI_excinfo *info)
17111712
{
1713+
assert(info != NULL);
17121714
_PyXI_excinfo_clear(info);
17131715
PyMem_RawFree(info);
17141716
}

0 commit comments

Comments
 (0)