Skip to content

Commit 66aa78c

Browse files
authored
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.
1 parent 5da379c commit 66aa78c

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Diff for: Lib/test/test_builtin.py

+10
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,16 @@ def test_filter_pickle(self):
926926
f2 = filter(filter_char, "abcdeabcde")
927927
self.check_iter_pickle(f1, list(f2), proto)
928928

929+
def test_filter_dealloc(self):
930+
# Tests recursive deallocation of nested filter objects using the
931+
# thrashcan mechanism. See gh-102356 for more details.
932+
max_iters = 1000000
933+
i = filter(bool, range(max_iters))
934+
for _ in range(max_iters):
935+
i = filter(bool, i)
936+
del i
937+
gc.collect()
938+
929939
def test_getattr(self):
930940
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
931941
self.assertRaises(TypeError, getattr)

Diff for: Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ Tim Golden
637637
Yonatan Goldschmidt
638638
Mark Gollahon
639639
Mikhail Golubev
640+
Marta Gómez Macías
640641
Guilherme Gonçalves
641642
Tiago Gonçalves
642643
Chris Gonnerman
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug that caused a crash when deallocating deeply nested filter
2+
objects. Patch by Marta Gómez Macías.

Diff for: Python/bltinmodule.c

+2
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,11 @@ static void
553553
filter_dealloc(filterobject *lz)
554554
{
555555
PyObject_GC_UnTrack(lz);
556+
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
556557
Py_XDECREF(lz->func);
557558
Py_XDECREF(lz->it);
558559
Py_TYPE(lz)->tp_free(lz);
560+
Py_TRASHCAN_END
559561
}
560562

561563
static int

0 commit comments

Comments
 (0)