]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] bpo-41909: Enable previously disabled recursion checks. (GH-22536) (GH-22551)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 4 Oct 2020 22:28:00 +0000 (01:28 +0300)
committerGitHub <noreply@github.com>
Sun, 4 Oct 2020 22:28:00 +0000 (01:28 +0300)
Enable recursion checks which were disabled when get __bases__ of
non-type objects in issubclass() and isinstance() and when intern
strings. It fixes a stack overflow when getting __bases__ leads
to infinite recursion.

Originally recursion checks was disabled for PyDict_GetItem() which
silences all errors including the one raised in case of detected
recursion and can return incorrect result. But now the code uses
PyDict_GetItemWithError() and PyDict_SetDefault() instead.
(cherry picked from commit 9ece9cd65cdeb0a1f6e60475bbd0219161c348ac)

Lib/test/test_isinstance.py
Misc/NEWS.d/next/Core and Builtins/2020-10-04-10-55-12.bpo-41909.BqHPcm.rst [new file with mode: 0644]
Objects/abstract.c
Objects/unicodeobject.c

index 53639e984e48a784cff38339c725ee1b9711be4e..31b38996930cc4e5d45f5ff5d39ee802fea4594f 100644 (file)
@@ -271,6 +271,16 @@ class TestIsInstanceIsSubclass(unittest.TestCase):
 
         self.assertEqual(True, issubclass(B(), int))
 
+    def test_infinite_recursion_in_bases(self):
+        class X:
+            @property
+            def __bases__(self):
+                return self.__bases__
+
+        self.assertRaises(RecursionError, issubclass, X(), int)
+        self.assertRaises(RecursionError, issubclass, int, X())
+        self.assertRaises(RecursionError, isinstance, 1, X())
+
 
 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/2020-10-04-10-55-12.bpo-41909.BqHPcm.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-04-10-55-12.bpo-41909.BqHPcm.rst
new file mode 100644 (file)
index 0000000..388cfea
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed stack overflow in :func:`issubclass` and :func:`isinstance` when
+getting the ``__bases__`` attribute leads to infinite recursion.
index 12237d570f7436a30ebe830c8642bce9d0798298..9c42d817b42111fb74a5b166cc2f0cc8b8403f6d 100644 (file)
@@ -2316,9 +2316,7 @@ abstract_get_bases(PyObject *cls)
     _Py_IDENTIFIER(__bases__);
     PyObject *bases;
 
-    Py_ALLOW_RECURSION
     (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases);
-    Py_END_ALLOW_RECURSION
     if (bases != NULL && !PyTuple_Check(bases)) {
         Py_DECREF(bases);
         return NULL;
index 4c2b42f959b838b56d4dc5f33af012d030fc3a6f..01df54da0051d816ace629b3ff17828016eda1ee 100644 (file)
@@ -15285,9 +15285,7 @@ PyUnicode_InternInPlace(PyObject **p)
             return;
         }
     }
-    Py_ALLOW_RECURSION
     t = PyDict_SetDefault(interned, s, s);
-    Py_END_ALLOW_RECURSION
     if (t == NULL) {
         PyErr_Clear();
         return;