summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend.aasland@protonmail.com>2023-06-09 17:56:02 (GMT)
committerGitHub <noreply@github.com>2023-06-09 17:56:02 (GMT)
commit4d4251d6eca0acd0761e0b75633a297cf20a30c0 (patch)
tree2a4c62105293f7457b3fbb526d13d5b415427111 /Modules
parent90e357bc8a1cafa2ddbea98949e6f6a4d7d01475 (diff)
downloadcpython-4d4251d6eca0acd0761e0b75633a297cf20a30c0.zip
cpython-4d4251d6eca0acd0761e0b75633a297cf20a30c0.tar.gz
cpython-4d4251d6eca0acd0761e0b75633a297cf20a30c0.tar.bz2
[3.11] gh-105375: Improve _pickle error handling (#105475) (#105583)
(cherry picked from commit 89aac6f6b7b3af046ec137121c90732289e79efc) Error handling was deferred in some cases, which could potentially lead to exceptions being overwritten.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_pickle.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 721079c..ddabc20 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1139,10 +1139,13 @@ _Pickler_New(void)
self->reducer_override = NULL;
self->memo = PyMemoTable_New();
+ if (self->memo == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
self->output_buffer = PyBytes_FromStringAndSize(NULL,
self->max_output_len);
-
- if (self->memo == NULL || self->output_buffer == NULL) {
+ if (self->output_buffer == NULL) {
Py_DECREF(self);
return NULL;
}
@@ -1627,9 +1630,12 @@ _Unpickler_New(void)
self->memo_size = 32;
self->memo_len = 0;
self->memo = _Unpickler_NewMemo(self->memo_size);
+ if (self->memo == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
self->stack = (Pdata *)Pdata_New();
-
- if (self->memo == NULL || self->stack == NULL) {
+ if (self->stack == NULL) {
Py_DECREF(self);
return NULL;
}
@@ -4833,11 +4839,12 @@ _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
PyObject *key, *value;
key = PyLong_FromVoidPtr(entry.me_key);
+ if (key == NULL) {
+ goto error;
+ }
value = Py_BuildValue("nO", entry.me_value, entry.me_key);
-
- if (key == NULL || value == NULL) {
- Py_XDECREF(key);
- Py_XDECREF(value);
+ if (value == NULL) {
+ Py_DECREF(key);
goto error;
}
status = PyDict_SetItem(new_memo, key, value);
@@ -6033,13 +6040,21 @@ load_stack_global(UnpicklerObject *self)
PyObject *global_name;
PDATA_POP(self->stack, global_name);
+ if (global_name == NULL) {
+ return -1;
+ }
PDATA_POP(self->stack, module_name);
- if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
- global_name == NULL || !PyUnicode_CheckExact(global_name)) {
+ if (module_name == NULL) {
+ Py_DECREF(global_name);
+ return -1;
+ }
+ if (!PyUnicode_CheckExact(module_name) ||
+ !PyUnicode_CheckExact(global_name))
+ {
PickleState *st = _Pickle_GetGlobalState();
PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
- Py_XDECREF(global_name);
- Py_XDECREF(module_name);
+ Py_DECREF(global_name);
+ Py_DECREF(module_name);
return -1;
}
global = find_class(self, module_name, global_name);