Skip to content

Commit 57a99d4

Browse files
gh-151842: Make _PyXI_FreeExcInfo NULL-safe to fix crash on capture_exception() OOM (OOM-0031)
1 parent 1de86e1 commit 57a99d4

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a segfault in :func:`_interpreters.capture_exception` under memory
2+
pressure. When ``_PyXI_NewExcInfo()`` failed to allocate, the cleanup path
3+
called ``_PyXI_FreeExcInfo(NULL)``, which then dereferenced offset 0 in
4+
``_excinfo_clear_type()``. ``_PyXI_FreeExcInfo()`` now accepts ``NULL`` like
5+
the surrounding ``PyMem_RawFree`` idiom. Patch by Amrutha Modela.

Python/crossinterp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,11 @@ _PyXI_NewExcInfo(PyObject *exc)
17091709
void
17101710
_PyXI_FreeExcInfo(_PyXI_excinfo *info)
17111711
{
1712+
if (info == NULL) {
1713+
// Matches the PyMem_RawFree(NULL) idiom: callers may pass NULL when
1714+
// _PyXI_NewExcInfo() failed (e.g. under OOM) before any allocation.
1715+
return;
1716+
}
17121717
_PyXI_excinfo_clear(info);
17131718
PyMem_RawFree(info);
17141719
}

0 commit comments

Comments
 (0)