Skip to content

Commit 60055d0

Browse files
serhiy-storchakamiss-islington
authored andcommitted
pythongh-126991: Add tests for unpickling bad object state (pythonGH-127031)
This catches a memory leak in loading the BUILD opcode. (cherry picked from commit addb225) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 7735f58 commit 60055d0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Lib/test/pickletester.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,41 @@ def test_bad_newobj_ex(self):
13441344
self.check_unpickling_error(error, b'cbuiltins\nint\nN}\x92.')
13451345
self.check_unpickling_error(error, b'cbuiltins\nint\n)N\x92.')
13461346

1347+
def test_bad_state(self):
1348+
c = C()
1349+
c.x = None
1350+
base = b'c__main__\nC\n)\x81'
1351+
self.assertEqual(self.loads(base + b'}X\x01\x00\x00\x00xNsb.'), c)
1352+
self.assertEqual(self.loads(base + b'N}X\x01\x00\x00\x00xNs\x86b.'), c)
1353+
# non-hashable dict key
1354+
self.check_unpickling_error(TypeError, base + b'}]Nsb.')
1355+
# state = list
1356+
error = (pickle.UnpicklingError, AttributeError)
1357+
self.check_unpickling_error(error, base + b'](}}eb.')
1358+
# state = 1-tuple
1359+
self.check_unpickling_error(error, base + b'}\x85b.')
1360+
# state = 3-tuple
1361+
self.check_unpickling_error(error, base + b'}}}\x87b.')
1362+
# non-hashable slot name
1363+
self.check_unpickling_error(TypeError, base + b'}}]Ns\x86b.')
1364+
# non-string slot name
1365+
self.check_unpickling_error(TypeError, base + b'}}NNs\x86b.')
1366+
# dict = True
1367+
self.check_unpickling_error(error, base + b'\x88}\x86b.')
1368+
# slots dict = True
1369+
self.check_unpickling_error(error, base + b'}\x88\x86b.')
1370+
1371+
class BadKey1:
1372+
count = 1
1373+
def __hash__(self):
1374+
if not self.count:
1375+
raise CustomError
1376+
self.count -= 1
1377+
return 42
1378+
__main__.BadKey1 = BadKey1
1379+
# bad hashable dict key
1380+
self.check_unpickling_error(CustomError, base + b'}c__main__\nBadKey1\n)\x81Nsb.')
1381+
13471382
def test_bad_stack(self):
13481383
badpickles = [
13491384
b'.', # STOP

0 commit comments

Comments
 (0)