Skip to content

Commit d4a04e5

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 <mgmacias@google.com>
1 parent d4992c7 commit d4a04e5

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Lib/test/test_builtin.py

+10
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,16 @@ def test_filter_pickle(self):
799799
f2 = filter(filter_char, "abcdeabcde")
800800
self.check_iter_pickle(f1, list(f2), proto)
801801

802+
def test_filter_dealloc(self):
803+
# Tests recursive deallocation of nested filter objects using the
804+
# thrashcan mechanism. See gh-102356 for more details.
805+
max_iters = 1000000
806+
i = filter(bool, range(max_iters))
807+
for _ in range(max_iters):
808+
i = filter(bool, i)
809+
del i
810+
gc.collect()
811+
802812
def test_getattr(self):
803813
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
804814
self.assertRaises(TypeError, getattr, sys, 1)

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ Tim Golden
627627
Yonatan Goldschmidt
628628
Mark Gollahon
629629
Mikhail Golubev
630+
Marta Gómez Macías
630631
Guilherme Gonçalves
631632
Tiago Gonçalves
632633
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.

Python/bltinmodule.c

+2
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,11 @@ static void
567567
filter_dealloc(filterobject *lz)
568568
{
569569
PyObject_GC_UnTrack(lz);
570+
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
570571
Py_XDECREF(lz->func);
571572
Py_XDECREF(lz->it);
572573
Py_TYPE(lz)->tp_free(lz);
574+
Py_TRASHCAN_END
573575
}
574576

575577
static int

0 commit comments

Comments
 (0)