]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30570: Use Py_EnterRecursiveCall() in issubclass() (GH-29048)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Fri, 22 Oct 2021 21:24:08 +0000 (17:24 -0400)
committerGitHub <noreply@github.com>
Fri, 22 Oct 2021 21:24:08 +0000 (14:24 -0700)
* Use Py_EnterRecursiveCall() in issubclass()

Reviewed-by: Gregory P. Smith <greg@krypto.org> [Google]
Lib/test/test_isinstance.py
Misc/NEWS.d/next/Core and Builtins/2021-10-19-01-04-08.bpo-30570._G30Ms.rst [new file with mode: 0644]
Objects/abstract.c

index 109c3f84a5c426dc45a1277bdc992b10421ae2e4..6ab44be9a26ba704b557bc400c9ca395e18671c0 100644 (file)
@@ -313,6 +313,36 @@ class TestIsInstanceIsSubclass(unittest.TestCase):
         self.assertRaises(RecursionError, issubclass, int, X())
         self.assertRaises(RecursionError, isinstance, 1, X())
 
+    def test_infinite_recursion_via_bases_tuple(self):
+        """Regression test for bpo-30570."""
+        class Failure(object):
+            def __getattr__(self, attr):
+                return (self, None)
+
+        with self.assertRaises(RecursionError):
+            issubclass(Failure(), int)
+
+    def test_infinite_cycle_in_bases(self):
+        """Regression test for bpo-30570."""
+        class X:
+            @property
+            def __bases__(self):
+                return (self, self, self)
+        self.assertRaises(RecursionError, issubclass, X(), int)
+
+    def test_infinitely_many_bases(self):
+        """Regression test for bpo-30570."""
+        class X:
+            def __getattr__(self, attr):
+                self.assertEqual(attr, "__bases__")
+                class A:
+                    pass
+                class B:
+                    pass
+                A.__getattr__ = B.__getattr__ = X.__getattr__
+                return (A(), B())
+        self.assertRaises(RecursionError, issubclass, X(), int)
+
 
 def blowstack(fxn, arg, compare_to):
     # Make sure that calling isinstance with a deeply nested tuple for its
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-10-19-01-04-08.bpo-30570._G30Ms.rst b/Misc/NEWS.d/next/Core and Builtins/2021-10-19-01-04-08.bpo-30570._G30Ms.rst
new file mode 100644 (file)
index 0000000..d9ab60c
--- /dev/null
@@ -0,0 +1 @@
+Fixed a crash in ``issubclass()`` from infinite recursion when searching pathological ``__bases__`` tuples.
\ No newline at end of file
index 6f7b94600e278adcd3c01ed172f50953314d5982..6227ad5a18bb95b277db48c7f6071b0b968c83fb 100644 (file)
@@ -2557,14 +2557,22 @@ abstract_issubclass(PyObject *derived, PyObject *cls)
             derived = PyTuple_GET_ITEM(bases, 0);
             continue;
         }
-        for (i = 0; i < n; i++) {
-            r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
-            if (r != 0)
-                break;
-        }
+        break;
+    }
+    assert(n >= 2);
+    if (Py_EnterRecursiveCall(" in __issubclass__")) {
         Py_DECREF(bases);
-        return r;
+        return -1;
     }
+    for (i = 0; i < n; i++) {
+        r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
+        if (r != 0) {
+            break;
+        }
+    }
+    Py_LeaveRecursiveCall();
+    Py_DECREF(bases);
+    return r;
 }
 
 static int