Commit cdbf50cb authored by Alexey Izbyshev's avatar Alexey Izbyshev Committed by Berker Peksag

bpo-34441: Fix ABC.__subclasscheck__ crash on classes with invalid __subclasses__ (GH-8835)

The missing NULL check was reported by Svace static analyzer.
parent 4c855577
......@@ -410,6 +410,36 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
with self.assertRaises(TypeError):
issubclass(C(), A)
# bpo-34441: Check that issubclass() doesn't crash on bogus
# classes.
bogus_subclasses = [
None,
lambda x: [],
lambda: 42,
lambda: [42],
]
for i, func in enumerate(bogus_subclasses):
class S(metaclass=abc_ABCMeta):
__subclasses__ = func
with self.subTest(i=i):
with self.assertRaises(TypeError):
issubclass(int, S)
# Also check that issubclass() propagates exceptions raised by
# __subclasses__.
exc_msg = "exception from __subclasses__"
def raise_exc():
raise Exception(exc_msg)
class S(metaclass=abc_ABCMeta):
__subclasses__ = raise_exc
with self.assertRaisesRegex(Exception, exc_msg):
issubclass(int, S)
def test_all_new_methods_are_called(self):
class A(metaclass=abc_ABCMeta):
pass
......
Fix crash when an ``ABC``-derived class with invalid ``__subclasses__`` is
passed as the second argument to :func:`issubclass()`. Patch by Alexey
Izbyshev.
......@@ -665,6 +665,9 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
/* 6. Check if it's a subclass of a subclass (recursive). */
subclasses = PyObject_CallMethod(self, "__subclasses__", NULL);
if (subclasses == NULL) {
goto end;
}
if (!PyList_Check(subclasses)) {
PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list");
goto end;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment