]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] bpo-33930: Fix segfault with deep recursion when cleaning method objects (GH...
authorŁukasz Langa <lukasz@langa.pl>
Wed, 11 Aug 2021 15:35:38 +0000 (17:35 +0200)
committerGitHub <noreply@github.com>
Wed, 11 Aug 2021 15:35:38 +0000 (17:35 +0200)
(cherry picked from commit bfc2d5a5c4550ab3a2fadeb9459b4bd948ff61a2)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2021-08-09-14-29-52.bpo-33930.--5LQ-.rst [new file with mode: 0644]
Objects/methodobject.c

index 457b46e01ce317b119db439562e58996d90be826..a29a1a7a523946457326419a2480bd85133108e4 100644 (file)
@@ -982,6 +982,21 @@ class ExceptionTests(unittest.TestCase):
         self.assertIsInstance(v, RecursionError, type(v))
         self.assertIn("maximum recursion depth exceeded", str(v))
 
+
+    @cpython_only
+    def test_crashcan_recursion(self):
+        # See bpo-33930
+
+        def foo():
+            o = object()
+            for x in range(1_000_000):
+                # Create a big chain of method objects that will trigger
+                # a deep chain of calls when they need to be destructed.
+                o = o.__dir__
+
+        foo()
+        support.gc_collect()
+
     @cpython_only
     def test_recursion_normalizing_exception(self):
         # Issue #22898.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-14-29-52.bpo-33930.--5LQ-.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-14-29-52.bpo-33930.--5LQ-.rst
new file mode 100644 (file)
index 0000000..827dd3f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix segmentation fault with deep recursion when cleaning method objects.
+Patch by Augusto Goulart and Pablo Galindo.
index 3604a55e5a1dc9458346a619f3811737aa698c31..940de8c326f1533992ad7c9f1127a844b8f46aaa 100644 (file)
@@ -125,7 +125,10 @@ PyCFunction_GetFlags(PyObject *op)
 static void
 meth_dealloc(PyCFunctionObject *m)
 {
-    _PyObject_GC_UNTRACK(m);
+    // The Py_TRASHCAN mechanism requires that we be able to
+    // call PyObject_GC_UnTrack twice on an object.
+    PyObject_GC_UnTrack(m);
+    Py_TRASHCAN_BEGIN(m, meth_dealloc);
     if (m->m_weakreflist != NULL) {
         PyObject_ClearWeakRefs((PyObject*) m);
     }
@@ -139,6 +142,7 @@ meth_dealloc(PyCFunctionObject *m)
     else {
         PyObject_GC_Del(m);
     }
+    Py_TRASHCAN_END;
 }
 
 static PyObject *