[Python-checkins] gh-102356: Add thrashcan macros to filter object dealloc (GH-102426)

miss-islington webhook-mailer at python.org
Sun Mar 5 06:20:55 EST 2023


https://github.com/python/cpython/commit/d4a04e55d8bd8eaa307f3e4aa8b443301a0d996a
commit: d4a04e55d8bd8eaa307f3e4aa8b443301a0d996a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-03-05T03:20:41-08:00
summary:

gh-102356: Add thrashcan macros to filter object dealloc (GH-102426)


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 at google.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst
M Lib/test/test_builtin.py
M Misc/ACKS
M Python/bltinmodule.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index aabf0ab5a92a..f31ab72c0209 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -799,6 +799,16 @@ def test_filter_pickle(self):
             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, sys, 1)
diff --git a/Misc/ACKS b/Misc/ACKS
index b4be8af3d99f..de1fb148e926 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -627,6 +627,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
index 000000000000..c03fd5266bc3
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst	
@@ -0,0 +1,2 @@
+Fix a bug that caused a crash when deallocating deeply nested filter
+objects. Patch by Marta Gómez Macías.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 659b78edc3de..6ea20bfc5f7c 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -567,9 +567,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



More information about the Python-checkins mailing list