Skip to content
Prev Previous commit
clean up test case, better name
  • Loading branch information
sweeneyde committed Jun 11, 2022
commit 370c44e650f01b2138e32590be7cbc271793b140
19 changes: 11 additions & 8 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,7 @@ def check_array(arr):
check_array(arr[::2])

def test_evil_class_mutating_dict(self):
# https://github.com/python/cpython/issues/92930
from random import getrandbits

global Bad
Expand Down Expand Up @@ -3064,25 +3065,27 @@ def __reduce__(self):
expected = "changed size during iteration"
self.assertIn(expected, str(e))

def test_evil_class_mutating_container(self):
def test_evil_pickler_mutating_collection(self):
# https://github.com/python/cpython/issues/92930
if not hasattr(self, "pickler"):
raise self.skipTest(f"{type(self)} has no associated pickler type")

global Clearer
class Clearer:
pass

def check(a):
class P(self.pickler):
def check(collection):
class EvilPickler(self.pickler):
def persistent_id(self, obj):
if isinstance(obj, Clearer):
a.clear()
collection.clear()
return None
pickler = EvilPickler(io.BytesIO(), proto)
try:
P(io.BytesIO(), proto).dump(a)
except RuntimeError:
# Don't care if this raises, just don't segfault.
pass
pickler.dump(collection)
except RuntimeError as e:
expected = "changed size during iteration"
self.assertIn(expected, str(e))

for proto in protocols:
check([Clearer()])
Expand Down