Skip to content

Commit

Permalink
gh-102356: Add thrashcan macros to filter object dealloc (GH-102426)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
miss-islington and mgmacias95 committed Mar 5, 2023
1 parent d4992c7 commit d4a04e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug that caused a crash when deallocating deeply nested filter
objects. Patch by Marta Gómez Macías.
2 changes: 2 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4a04e5

Please sign in to comment.