Skip to content

Commit cc36604

Browse files
authored
gh-159739: Fully clear the unpickler memo (#160173)
Clearing an Unpickler memo left its sequential index and sparse entries intact, so reusing the unpickler for an independent record could raise UnpicklingError or retrieve an object from a previous record. Replace the memo and reset all of its state before releasing the old entries. Backport to 3.15; manually backport only the counter reset to 3.14, which has no sparse memo.
1 parent 0b8bb71 commit cc36604

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎Lib/test/pickletester.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,6 +5299,22 @@ def test_clear_pickler_memo(self):
52995299
self.assertNotEqual(first_pickled, second_pickled)
53005300
self.assertEqual(first_pickled, third_pickled)
53015301

5302+
def test_clear_unpickler_memo(self):
5303+
value = [b'repeated'] * 2
5304+
data = pickle.dumps(value, protocol=4)
5305+
unpickler = self.unpickler_class(io.BytesIO(data * 2))
5306+
self.assertEqual(unpickler.load(), value)
5307+
unpickler.memo.clear()
5308+
self.assertEqual(unpickler.load(), value)
5309+
5310+
def test_clear_unpickler_memo_sparse(self):
5311+
# Store None at index 1000, then retrieve it in a second record.
5312+
unpickler = self.unpickler_class(io.BytesIO(b'Np1000\n.g1000\n.'))
5313+
self.assertIsNone(unpickler.load())
5314+
unpickler.memo.clear()
5315+
with self.assertRaises(pickle.UnpicklingError):
5316+
unpickler.load()
5317+
53025318
def test_priming_pickler_memo(self):
53035319
# Verify that we can set the Pickler's memo attribute.
53045320
data = ["abcdefg", "abcdefg", 44]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix clearing a :class:`pickle.Unpickler` memo so that the unpickler can be
2+
reused for independent records.

‎Modules/_pickle.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,16 +1762,16 @@ _Unpickler_NewMemo(Py_ssize_t new_size)
17621762
return memo;
17631763
}
17641764

1765-
/* Free the unpickler's memo, taking care to decref any items left in it. */
1765+
/* Replace the unpickler's memo and release the old entries. */
17661766
static void
1767-
_Unpickler_MemoCleanup(UnpicklerObject *self)
1767+
_Unpickler_MemoCleanup(UnpicklerObject *self, PyObject **replacement)
17681768
{
17691769
Py_ssize_t i;
17701770
PyObject **memo = self->memo;
17711771

1772-
if (self->memo == NULL)
1772+
self->memo = replacement;
1773+
if (memo == NULL)
17731774
return;
1774-
self->memo = NULL;
17751775
i = self->memo_size;
17761776
while (--i >= 0) {
17771777
Py_XDECREF(memo[i]);
@@ -7769,7 +7769,7 @@ Unpickler_clear(PyObject *op)
77697769
}
77707770

77717771
Py_CLEAR(self->memo_dict);
7772-
_Unpickler_MemoCleanup(self);
7772+
_Unpickler_MemoCleanup(self, NULL);
77737773
PyMem_Free(self->marks);
77747774
self->marks = NULL;
77757775
PyMem_Free(self->input_line);
@@ -7916,10 +7916,16 @@ static PyObject *
79167916
_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
79177917
/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
79187918
{
7919-
_Unpickler_MemoCleanup(self->unpickler);
7920-
self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
7921-
if (self->unpickler->memo == NULL)
7919+
UnpicklerObject *unpickler = self->unpickler;
7920+
PyObject **memo = _Unpickler_NewMemo(unpickler->memo_size);
7921+
if (memo == NULL) {
79227922
return NULL;
7923+
}
7924+
PyObject *memo_dict = unpickler->memo_dict;
7925+
unpickler->memo_dict = NULL;
7926+
unpickler->memo_len = 0;
7927+
_Unpickler_MemoCleanup(unpickler, memo);
7928+
Py_XDECREF(memo_dict);
79237929
Py_RETURN_NONE;
79247930
}
79257931

@@ -8124,7 +8130,7 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
81248130
return -1;
81258131
}
81268132

8127-
_Unpickler_MemoCleanup(self);
8133+
_Unpickler_MemoCleanup(self, NULL);
81288134
self->memo_size = new_memo_size;
81298135
self->memo = new_memo;
81308136

0 commit comments

Comments
 (0)