Skip to content

gh-159294: Fix crash when creating an ExceptionGroup fails with MemoryError - #159295

Merged
StanFromIreland merged 2 commits into
python:mainfrom
christianaurichzm:gh-159294-exceptiongroup-no-memory
Oct 23, 2026
Merged

StanFromIreland merged 2 commits into
python:mainfrom
christianaurichzm:gh-159294-exceptiongroup-no-memory

Conversation

@christianaurichzm

@christianaurichzm christianaurichzm commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #159294

BaseExceptionGroup_new() passed the result of BaseException_new() to PyBaseExceptionGroupObject_CAST() before checking it for NULL. The cast asserts _PyBaseExceptionGroup_Check() on its argument, so when the allocation fails a debug build dereferences NULL instead of raising MemoryError. Release builds are unchanged, since they already returned NULL with MemoryError set.

This change checks the result before the cast.

Verification

  • The new test in test_exception_group segfaults an unpatched debug build and passes with the fix, also with -R 3:3.
  • test_exception_group, test_exceptions, test_except_star and test_traceback pass on a debug build.

The test uses _testcapi.call_with_nomemory(), which only exists on main, so the 3.15 and 3.14 backports will need to adapt it.

… MemoryError

BaseExceptionGroup_new() passed the result of BaseException_new() to
PyBaseExceptionGroupObject_CAST() before checking it for NULL. The cast
asserts the type of its argument, so a debug build dereferenced NULL
instead of raising MemoryError.

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@christianaurichzm

Copy link
Copy Markdown
Contributor Author

Hi @iritkatriel, just checking in on this PR. With approvals and green CI, is there anything else needed before merging?

The fix should backport cleanly to 3.14 and 3.15, only the regression test needs adapting since _testcapi.call_with_nomemory() is main-only. Happy to handle the backports once this lands.

Thanks!

@StanFromIreland
StanFromIreland enabled auto-merge (squash) October 23, 2026 19:32
@StanFromIreland

Copy link
Copy Markdown
Member

I fixed a little nit and enabled auto-merge.

@StanFromIreland StanFromIreland added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 23, 2026
@StanFromIreland
StanFromIreland merged commit 2507a7f into python:main Oct 23, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @christianaurichzm for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

GH-160504 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 23, 2026
@bedevere-app

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

GH-160505 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 23, 2026
StanFromIreland pushed a commit that referenced this pull request Oct 23, 2026
…ith `MemoryError` (GH-159295) (#160504)

(cherry picked from commit 2507a7f)

Co-authored-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating an ExceptionGroup segfaults on MemoryError in debug builds

3 participants