Skip to content

Commit

Permalink
pythongh-102356: Add thrashcan macros to filter object dealloc (pytho…
Browse files Browse the repository at this point in the history
…nGH-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>
  • Loading branch information
mgmacias95 authored and miss-islington committed Mar 5, 2023
1 parent caff048 commit 060e8ac
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 @@ -919,6 +919,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)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,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 @@ -556,9 +556,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 060e8ac

Please sign in to comment.