Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -5299,6 +5299,22 @@ def test_clear_pickler_memo(self):
self.assertNotEqual(first_pickled, second_pickled)
self.assertEqual(first_pickled, third_pickled)

def test_empty_unpickler_memo(self):
# gh-162334: assigning an empty memo used to crash the C unpickler
# on the next memoized object.
for proto in protocols:
with self.subTest(proto=proto):
value = [b'repeated'] * 2
data = pickle.dumps(value, protocol=proto)
unpickler = self.unpickler_class(io.BytesIO(data))
unpickler.memo = {}
result = unpickler.load()
self.assertEqual(result, value)
self.assertIs(result[0], result[1])
unpickler = self.unpickler_class(io.BytesIO(b'\x80\x04N\x94.'))
unpickler.memo = {}
self.assertIsNone(unpickler.load())

def test_clear_unpickler_memo(self):
value = [b'repeated'] * 2
data = pickle.dumps(value, protocol=4)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in the C-accelerated :class:`pickle.Unpickler` when assigning
an empty memo dictionary before processing a memoized object.
4 changes: 3 additions & 1 deletion Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,9 @@ _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Py_DECREF(key);
return 0;
}
if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
/* idx is 0 if the memo is empty, e.g. after "memo = {}" (gh-162334);
start again from the initial memo size then. */
if (_Unpickler_ResizeMemoList(self, idx ? idx * 2 : 32) < 0)
return -1;
assert(idx < self->memo_size);
}
Expand Down
Loading