mirror of https://github.com/python/cpython
gh-92112: Fix crash triggered by an evil custom `mro()` (#92113)
This commit is contained in:
parent
adcb6a6055
commit
85354ed78c
|
@ -5784,6 +5784,23 @@ class MroTest(unittest.TestCase):
|
||||||
class A(metaclass=M):
|
class A(metaclass=M):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def test_disappearing_custom_mro(self):
|
||||||
|
"""
|
||||||
|
gh-92112: A custom mro() returning a result conflicting with
|
||||||
|
__bases__ and deleting itself caused a double free.
|
||||||
|
"""
|
||||||
|
class B:
|
||||||
|
pass
|
||||||
|
|
||||||
|
class M(DebugHelperMeta):
|
||||||
|
def mro(cls):
|
||||||
|
del M.mro
|
||||||
|
return (B,)
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
class A(metaclass=M):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix crash triggered by an evil custom ``mro()`` on a metaclass.
|
|
@ -345,22 +345,26 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
|
||||||
Py_ssize_t i, n;
|
Py_ssize_t i, n;
|
||||||
int custom = !Py_IS_TYPE(type, &PyType_Type);
|
int custom = !Py_IS_TYPE(type, &PyType_Type);
|
||||||
int unbound;
|
int unbound;
|
||||||
PyObject *mro_meth = NULL;
|
|
||||||
PyObject *type_mro_meth = NULL;
|
|
||||||
|
|
||||||
if (custom) {
|
if (custom) {
|
||||||
|
PyObject *mro_meth, *type_mro_meth;
|
||||||
mro_meth = lookup_maybe_method(
|
mro_meth = lookup_maybe_method(
|
||||||
(PyObject *)type, &_Py_ID(mro), &unbound);
|
(PyObject *)type, &_Py_ID(mro), &unbound);
|
||||||
if (mro_meth == NULL)
|
if (mro_meth == NULL) {
|
||||||
goto clear;
|
goto clear;
|
||||||
|
}
|
||||||
type_mro_meth = lookup_maybe_method(
|
type_mro_meth = lookup_maybe_method(
|
||||||
(PyObject *)&PyType_Type, &_Py_ID(mro), &unbound);
|
(PyObject *)&PyType_Type, &_Py_ID(mro), &unbound);
|
||||||
if (type_mro_meth == NULL)
|
if (type_mro_meth == NULL) {
|
||||||
|
Py_DECREF(mro_meth);
|
||||||
goto clear;
|
goto clear;
|
||||||
if (mro_meth != type_mro_meth)
|
}
|
||||||
|
int custom_mro = (mro_meth != type_mro_meth);
|
||||||
|
Py_DECREF(mro_meth);
|
||||||
|
Py_DECREF(type_mro_meth);
|
||||||
|
if (custom_mro) {
|
||||||
goto clear;
|
goto clear;
|
||||||
Py_XDECREF(mro_meth);
|
}
|
||||||
Py_XDECREF(type_mro_meth);
|
|
||||||
}
|
}
|
||||||
n = PyTuple_GET_SIZE(bases);
|
n = PyTuple_GET_SIZE(bases);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
|
@ -373,8 +377,6 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
clear:
|
clear:
|
||||||
Py_XDECREF(mro_meth);
|
|
||||||
Py_XDECREF(type_mro_meth);
|
|
||||||
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
|
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
|
||||||
type->tp_version_tag = 0; /* 0 is not a valid version tag */
|
type->tp_version_tag = 0; /* 0 is not a valid version tag */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue