Repository navigation
[mypyc] Fix memory safety bugs in list.count, match sequence patterns and dict.setdefault - #22415
Merged
JukkaL merged 3 commits intoNov 10, 2026
Merged
Conversation
The sequence_get_item primitive (PySequence_GetItem) used by sequence patterns in match statements was declared with ERR_NEVER, so if the subject's __getitem__ raised an exception the NULL result was used as a valid object, causing a crash. Use ERR_MAGIC so the error propagates.
On builds with the GIL, list_count passed the borrowed list item directly to PyObject_RichCompareBool. If the item's __eq__ removed it from the list (e.g. by clearing the list), the item could be freed while still in use, for example by the reflected comparison. Hold a reference to the item during the comparison, like CPython does.
CPyDict_SetDefault used PyDict_SetDefault, which returns a borrowed
reference, and only increfed it after the call. On free-threaded builds
another thread can remove the value from the dict in between, so the
result could be freed before we took a reference. Use
PyDict_SetDefaultRef instead.
CPyDict_SetDefaultWithEmptyDatatype (used for d.setdefault(k, []),
d.setdefault(k, {}) and d.setdefault(k, set())) did a separate get and
set. This was not atomic on free-threaded builds, so concurrent calls
could each insert a fresh collection and lose updates. For dict
subclasses it also used __getitem__, so __missing__ was triggered
(e.g. Counter returned 0 and inserted nothing) and an overridden
setdefault was ignored. Now we only do a lookup as a fast path for
exact dicts, and otherwise create the empty collection and call
setdefault (PyDict_SetDefaultRef for exact dicts, the setdefault method
for subclasses).
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.
This fixes three crashes in mypyc container primitives, and an incorrect result for
dict.setdefaulton dict subclasses.Match sequence patterns.
PySequence_GetItemwas declared as never failing, so a__getitem__that raises crashed compiled code:It is now declared with
ERR_MAGIC.list.count. On GIL builds, it passed a borrowed item toPyObject_RichCompareBool. If__eq__removes the item from the list (e.g.lst.clear(); return NotImplemented), the reflected comparison receives a freed object. It now holds a reference during the comparison, as CPython does.dict.setdefault(k, v)on free-threaded builds. It usedPyDict_SetDefault, which returns a borrowed reference, and only increfed the result after the dict lock was released. A stress test segfaults reliably on 3.14t. It now usesPyDict_SetDefaultRef.d.setdefault(k, [] / {} / set()). This did a separate get and set:__getitem__, so__missing__ran and asetdefaultoverride was ignored. For example,Counter().setdefault("x", [])returned0and inserted nothing.It now keeps the lookup fast path for exact dicts. Otherwise it creates the empty collection and calls
setdefault.Added regression tests to
run-match.test,run-lists.testandrun-dicts.test. The free-threading races can't be tested deterministically, so I checked them manually with stress scripts on 3.14t: before the fix they segfaulted or lost updates, after it they ran clean.