Repository navigation
gh-159285: Fix crash when copying an empty dict fails with MemoryError - #159286
Merged
methane merged 1 commit intoOct 13, 2026
Merged
Conversation
…ryError copy_lock_held_untracked() asserted that the new empty dict was not tracked by the GC without checking whether its allocation had failed, so a debug build dereferenced NULL instead of raising MemoryError. This affected dict.copy(), PyDict_Copy() and _PyDict_CopyAsDict(), which type() uses for the class namespace.
christianaurichzm
requested review from
markshannon and
methane
as code owners
October 13, 2026 02:37
methane
approved these changes
Oct 13, 2026
|
Thanks @christianaurichzm for the PR, and @methane for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-159287 is a backport of this pull request to the 3.15 branch. |
methane
pushed a commit
that referenced
this pull request
Oct 13, 2026
GH-159286) copy_lock_held_untracked() asserted that the new empty dict was not tracked by the GC without checking whether its allocation had failed, so a debug build dereferenced NULL instead of raising MemoryError. This affected dict.copy(), PyDict_Copy() and _PyDict_CopyAsDict(), which type() uses for the class namespace. (cherry picked from commit f9132f1) Co-authored-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #159285
copy_lock_held_untracked()handles the empty-dict case by allocating a new dict and then asserting!_PyObject_GC_IS_TRACKED(d). When the allocation fails,disNULLand the assertion dereferences it, so a debug build segfaults instead of raisingMemoryError. This is reachable fromdict.copy(),PyDict_Copy()and_PyDict_CopyAsDict(), whichtype()uses for the class namespace.This change lets the assertion accept
NULL. Release builds are unchanged, since they already returnedNULLwithMemoryErrorset.Verification
test_dictsegfaults an unpatched debug build and passes with the fix, also with-R 3:3.test_dict,test_capi.test_dict,test_class,test_descr,test_typesandtest_gcpass on a debug build.