]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102356: Add thrashcan macros to filter object dealloc (#102426)
authorMarta Gómez Macías <mgmacias@google.com>
Sun, 5 Mar 2023 11:00:41 +0000 (12:00 +0100)
committerGitHub <noreply@github.com>
Sun, 5 Mar 2023 11:00:41 +0000 (12:00 +0100)
Add thrashcan macros to the deallocator of the filter objects to protect against deeply nested destruction of chains of nested filters.

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 9e19af0ae90fc1b3d82f3f09213bbe5d637cccf2..e7a79bc13b7f3d95a0f2e8cfb289b66fef6c1dfb 100644 (file)
@@ -926,6 +926,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 c591cd3bfe4b9e1b393b0a2f92bf75c64eceec36..7bbde3af99782bb48d3f1be49c9ad8d752c71395 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -637,6 +637,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 53439ab16040c4f2476e88f451324906205ba925..12ca0ba6c4873cd9cee5450bcfd795e476b274b3 100644 (file)
@@ -553,9 +553,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