]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102356: Add thrashcan macros to filter object dealloc (GH-102426)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 5 Mar 2023 11:25:48 +0000 (03:25 -0800)
committerGitHub <noreply@github.com>
Sun, 5 Mar 2023 11:25:48 +0000 (03:25 -0800)
Add thrashcan macros to the deallocator of the filter objects to protect against deeply nested destruction of chains of nested filters.
(cherry picked from commit 66aa78cbe604a7c5731f074b869f92174a8e3b64)

Co-authored-by: Marta Gómez Macías <mgmacias@google.com>
Lib/test/test_builtin.py
Misc/ACKS
Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst [new file with mode: 0644]
Python/bltinmodule.c

index 6cd72eda774c2a47f5a81544f5a2d0a65b9421d7..0f6d2db0ecb6d4ec3e8b41b9a35041bb4d97fe28 100644 (file)
@@ -919,6 +919,16 @@ class BuiltinTest(unittest.TestCase):
             f2 = filter(filter_char, "abcdeabcde")
             self.check_iter_pickle(f1, list(f2), proto)
 
+    def test_filter_dealloc(self):
+        # Tests recursive deallocation of nested filter objects using the
+        # thrashcan mechanism. See gh-102356 for more details.
+        max_iters = 1000000
+        i = filter(bool, range(max_iters))
+        for _ in range(max_iters):
+            i = filter(bool, i)
+        del i
+        gc.collect()
+
     def test_getattr(self):
         self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
         self.assertRaises(TypeError, getattr)
index 22df1b998fb15ac79adceba019e5b0e62f859cf9..afe86d37b0a707ca3fc0215f74cbe40ea862ebcd 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -631,6 +631,7 @@ Tim Golden
 Yonatan Goldschmidt
 Mark Gollahon
 Mikhail Golubev
+Marta Gómez Macías
 Guilherme Gonçalves
 Tiago Gonçalves
 Chris Gonnerman
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst b/Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst
new file mode 100644 (file)
index 0000000..c03fd52
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a bug that caused a crash when deallocating deeply nested filter
+objects. Patch by Marta Gómez Macías.
index a3fdb394a13680b50d9ed255fa66588a32fa2fc5..6c8725f98231133ac0687994dbc371ff704e208c 100644 (file)
@@ -556,9 +556,11 @@ static void
 filter_dealloc(filterobject *lz)
 {
     PyObject_GC_UnTrack(lz);
+    Py_TRASHCAN_BEGIN(lz, filter_dealloc)
     Py_XDECREF(lz->func);
     Py_XDECREF(lz->it);
     Py_TYPE(lz)->tp_free(lz);
+    Py_TRASHCAN_END
 }
 
 static int