diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index e9d367f9ea95aa..270bdc62fc5ba0 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst b/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst new file mode 100644 index 00000000000000..2af673ba1c5519 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst @@ -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. diff --git a/Modules/_pickle.c b/Modules/_pickle.c index b8f9b1c92f2072..8666358d8ec82e 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -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); }