summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarta Gómez Macías <mgmacias@google.com>2023-03-05 11:00:41 (GMT)
committerGitHub <noreply@github.com>2023-03-05 11:00:41 (GMT)
commit66aa78cbe604a7c5731f074b869f92174a8e3b64 (patch)
tree497aee1993fce7be009427638f48da111a9ecdc4
parent5da379ca7dff44b321450800252be01041b3320b (diff)
downloadcpython-66aa78cbe604a7c5731f074b869f92174a8e3b64.zip
cpython-66aa78cbe604a7c5731f074b869f92174a8e3b64.tar.gz
cpython-66aa78cbe604a7c5731f074b869f92174a8e3b64.tar.bz2
gh-102356: Add thrashcan macros to filter object dealloc (#102426)
Add thrashcan macros to the deallocator of the filter objects to protect against deeply nested destruction of chains of nested filters.
-rw-r--r--Lib/test/test_builtin.py10
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2023-03-04-20-56-12.gh-issue-102356.07KvUd.rst2
-rw-r--r--Python/bltinmodule.c2
4 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 9e19af0..e7a79bc 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -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)
diff --git a/Misc/ACKS b/Misc/ACKS
index c591cd3..7bbde3a 100644
--- 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
index 0000000..c03fd52
--- /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 53439ab..12ca0ba 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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