summaryrefslogtreecommitdiff
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-11-24 20:15:08 +0100
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-11-24 11:15:08 -0800
commit6f03b236c17c96bc9f8a004ffa7e7ae0542e9cac (patch)
treea3c37873c66b49cfe26b7b7183b75adead85b37b /Modules/_pickle.c
parente407646b741db6851789963e525a1f5daad0a336 (diff)
downloadcpython-git-6f03b236c17c96bc9f8a004ffa7e7ae0542e9cac.tar.gz
bpo-38876: Raise pickle.UnpicklingError when loading an item from memo for invalid input (GH-17335)
The previous code was raising a `KeyError` for both the Python and C implementation. This was caused by the specified index of an invalid input which did not exist in the memo structure, where the pickle stores what objects it has seen. The malformed input would have caused either a `BINGET` or `LONG_BINGET` load from the memo, leading to a `KeyError` as the determined index was bogus. https://bugs.python.org/issue38876 https://bugs.python.org/issue38876
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 7370be5938..baa0a27419 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -6174,8 +6174,10 @@ load_get(UnpicklerObject *self)
value = _Unpickler_MemoGet(self, idx);
if (value == NULL) {
- if (!PyErr_Occurred())
- PyErr_SetObject(PyExc_KeyError, key);
+ if (!PyErr_Occurred()) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
+ }
Py_DECREF(key);
return -1;
}
@@ -6201,7 +6203,8 @@ load_binget(UnpicklerObject *self)
if (value == NULL) {
PyObject *key = PyLong_FromSsize_t(idx);
if (key != NULL) {
- PyErr_SetObject(PyExc_KeyError, key);
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
Py_DECREF(key);
}
return -1;
@@ -6227,7 +6230,8 @@ load_long_binget(UnpicklerObject *self)
if (value == NULL) {
PyObject *key = PyLong_FromSsize_t(idx);
if (key != NULL) {
- PyErr_SetObject(PyExc_KeyError, key);
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
Py_DECREF(key);
}
return -1;