summaryrefslogtreecommitdiff
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-07-17 11:35:35 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2016-07-17 11:35:35 +0300
commit3410c01d83d70ceef48b735c247aebd2d5bd3411 (patch)
tree31bd2f79a937309132d92e75ae53c25d5309bbbd /Modules/_pickle.c
parentfb2125daf35f7f21d04b5c567c639c20b0565a9c (diff)
parentdec25afab1c325c28621dda3ba2b32dbc200c8b3 (diff)
downloadcpython-git-3410c01d83d70ceef48b735c247aebd2d5bd3411.tar.gz
Issue #17711: Fixed unpickling by the persistent ID with protocol 0.
Original patch by Alexandre Vassalotti.
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 45f36249eb..ddac24334d 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -3440,26 +3440,30 @@ save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
goto error;
}
else {
- PyObject *pid_str = NULL;
- char *pid_ascii_bytes;
- Py_ssize_t size;
+ PyObject *pid_str;
pid_str = PyObject_Str(pid);
if (pid_str == NULL)
goto error;
- /* XXX: Should it check whether the persistent id only contains
- ASCII characters? And what if the pid contains embedded
+ /* XXX: Should it check whether the pid contains embedded
newlines? */
- pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
- Py_DECREF(pid_str);
- if (pid_ascii_bytes == NULL)
+ if (!PyUnicode_IS_ASCII(pid_str)) {
+ PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
+ "persistent IDs in protocol 0 must be "
+ "ASCII strings");
+ Py_DECREF(pid_str);
goto error;
+ }
if (_Pickler_Write(self, &persid_op, 1) < 0 ||
- _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
- _Pickler_Write(self, "\n", 1) < 0)
+ _Pickler_Write(self, PyUnicode_DATA(pid_str),
+ PyUnicode_GET_LENGTH(pid_str)) < 0 ||
+ _Pickler_Write(self, "\n", 1) < 0) {
+ Py_DECREF(pid_str);
goto error;
+ }
+ Py_DECREF(pid_str);
}
status = 1;
}
@@ -5477,9 +5481,15 @@ load_persid(UnpicklerObject *self)
if (len < 1)
return bad_readline();
- pid = PyBytes_FromStringAndSize(s, len - 1);
- if (pid == NULL)
+ pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
+ if (pid == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
+ PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
+ "persistent IDs in protocol 0 must be "
+ "ASCII strings");
+ }
return -1;
+ }
/* This does not leak since _Pickle_FastCall() steals the reference
to pid first. */