gh-151912: Fix segfault in type() with NULL tp_new metaclasses#151916
Open
Santhosh-I wants to merge 4 commits into
Open
gh-151912: Fix segfault in type() with NULL tp_new metaclasses#151916Santhosh-I wants to merge 4 commits into
type() with NULL tp_new metaclasses#151916Santhosh-I wants to merge 4 commits into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Pull request overview
Fixes a crash in CPython’s type(name, bases, namespace) code path when metaclass resolution selects a metaclass whose tp_new slot is NULL (i.e., not instantiable), by raising a TypeError instead of calling through a NULL function pointer.
Changes:
- Add a
winner->tp_new == NULLguard before delegating to a non-type_newmetaclass constructor. - Raise
TypeError: cannot create '<metaclass>' instancesrather than segfaulting.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+5036
to
+5038
| PyErr_Format(PyExc_TypeError, | ||
| "cannot create '%.400s' instances", | ||
| winner->tp_name); |
Comment on lines
+5034
to
5043
| /* Check if tp_new is NULL (cannot instantiate this type) */ | ||
| if (winner->tp_new == NULL) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "cannot create '%.400s' instances", | ||
| winner->tp_name); | ||
| return -1; | ||
| } | ||
| /* Pass it to the winner */ | ||
| *type = winner->tp_new(winner, ctx->args, ctx->kwds); | ||
| if (*type == NULL) { |
type() with NULL tp_new metaclasses
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.
Summary
Fix a crash in
type(...)when the provided metaclass hastp_new == NULL.Currently,
type(name, bases, namespace)can reach a code path that calls:without verifying that
winner->tp_newis non-NULL. For metaclasses that are valid base classes at the C level but cannot be instantiated from Python (tp_new == NULL), this results in a segmentation fault.This change adds validation to reject such metaclasses and raise a Python exception instead.
Bug
For example, on NumPy main:
where
metaclass_instance == np.dtype, can eventually dereference a NULLtp_newpointer.The expected behavior is consistent with:
which raises:
Fix
winner) has a validtp_new.TypeErrorwhentp_new == NULLinstead of attempting to call it.Issue
Closes gh-151912. #151912