Skip to content

Creating an ExceptionGroup segfaults on MemoryError in debug builds #159294

Closed

Description

@christianaurichzm

Crash report

Crash report

BaseExceptionGroup_new() in Objects/exceptions.c passes the result of BaseException_new() straight to PyBaseExceptionGroupObject_CAST():

PyBaseExceptionGroupObject *self =
    PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds));
if (!self) {
    goto error;
}

The cast asserts _PyBaseExceptionGroup_Check(exc), which dereferences its argument, so when BaseException_new() fails with MemoryError a debug build segfaults before reaching the NULL check.

import _testcapi

_testcapi.call_with_nomemory(0, 0, ExceptionGroup, "g", (ValueError(1),))

On main (627eabf) built with --with-pydebug, this segfaults:

#0  PyObject_TypeCheck (ob=ob@entry=0x0, type=0x555555b82380 <_PyExc_BaseExceptionGroup>) at ./Include/object.h:375
#1  0x00005555556a35c0 in PyBaseExceptionGroupObject_CAST (exc=0x0) at Objects/exceptions.c:861
#2  0x00005555556a59e3 in BaseExceptionGroup_new (type=<optimized out>, args=0x7ffff736d910, kwds=0x0) at Objects/exceptions.c:977

Release builds raise MemoryError as expected. The cast came with gh-111178, and the same code is in 3.14 and 3.15.

Checking the result of BaseException_new() before casting it fixes the crash.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

  1. srinivasreddy commented on Oct 13, 2026

    @srinivasreddy
    Contributor

    I can confirm the bug on windows too.

    D:\workspace\cpython (main -> origin)
    λ python.bat
    Running Debug|x64 interpreter...
    Python 3.16.0a0 (heads/main:627eabf31cf, Oct 13 2026, 12:50:19) [MSC v.1944 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import _testcapi
    >>> _testcapi.call_with_nomemory(0, 0, ExceptionGroup, "g", (ValueError(1),))
    
  2. added 2 commits that reference this issue on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions