Skip to content

Do not decref borrowed module dict reference - #264

Merged
ndgrigorian merged 2 commits into
mainfrom
fix-module-dict-overdecref
Aug 31, 2026
Merged

Do not decref borrowed module dict reference#264
ndgrigorian merged 2 commits into
mainfrom
fix-module-dict-overdecref

Conversation

@antonwolfy

Copy link
Copy Markdown
Collaborator

Summary

PyModule_GetDict() returns a borrowed reference. In PyInit__ufuncs, the InitOperators() error path called Py_XDECREF(d) on that borrowed reference, dropping the module dict's refcount one too many. This can free the dict prematurely and lead to a use-after-free (e.g. on the next access or during interpreter shutdown).

Only the module object m — the owned reference from PyModule_Create() — needs to be released on this error path. This removes the erroneous Py_XDECREF(d).

Notes

Latent, pre-existing bug; only reachable when InitOperators() fails at module init.

PyModule_GetDict() returns a borrowed reference. The InitOperators()
error path called Py_XDECREF(d) on it, dropping the module dict's
refcount one too many. That can free the dict prematurely and lead to
a use-after-free. Remove the erroneous decref; only the owned module
object m needs releasing on this path.
ndgrigorian
ndgrigorian previously approved these changes Aug 31, 2026

@ndgrigorian ndgrigorian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@ndgrigorian
ndgrigorian merged commit 79dc27c into main Aug 31, 2026
61 checks passed
@ndgrigorian
ndgrigorian deleted the fix-module-dict-overdecref branch August 31, 2026 20:57
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.

2 participants