Skip to content

Commit 63fd954

Browse files
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 66aa78c) Co-authored-by: Marta Gómez Macías <[email protected]>
1 parent caff048 commit 63fd954

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Lib/test/test_builtin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,16 @@ def test_filter_pickle(self):
919919
f2 = filter(filter_char, "abcdeabcde")
920920
self.check_iter_pickle(f1, list(f2), proto)
921921

922+
def test_filter_dealloc(self):
923+
# Tests recursive deallocation of nested filter objects using the
924+
# thrashcan mechanism. See gh-102356 for more details.
925+
max_iters = 1000000
926+
i = filter(bool, range(max_iters))
927+
for _ in range(max_iters):
928+
i = filter(bool, i)
929+
del i
930+
gc.collect()
931+
922932
def test_getattr(self):
923933
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
924934
self.assertRaises(TypeError, getattr)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ Tim Golden
631631
Yonatan Goldschmidt
632632
Mark Gollahon
633633
Mikhail Golubev
634+
Marta Gómez Macías
634635
Guilherme Gonçalves
635636
Tiago Gonçalves
636637
Chris Gonnerman
Lines changed: 2 additions & 0 deletions
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.

Python/bltinmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,11 @@ static void
556556
filter_dealloc(filterobject *lz)
557557
{
558558
PyObject_GC_UnTrack(lz);
559+
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
559560
Py_XDECREF(lz->func);
560561
Py_XDECREF(lz->it);
561562
Py_TYPE(lz)->tp_free(lz);
563+
Py_TRASHCAN_END
562564
}
563565

564566
static int

0 commit comments

Comments
 (0)