From c8b3427c5dc27029dd8a8ff580c9b9d1b5e3b185 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 5 Aug 2024 22:35:18 +0300 Subject: [PATCH 1/2] Fix reference leak --- Modules/_pickle.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 5d9ee8cb6c679d..5d6b5bb9bc5db6 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1959,18 +1959,21 @@ whichmodule(PickleState *st, PyObject *global, PyObject *global_name, PyObject * extra parameters of __import__ to fix that. */ module = PyImport_Import(module_name); if (module == NULL) { + Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: import of module %R failed", global, module_name); return NULL; } if (check_dotted_path(module, global_name, dotted_path) < 0) { + Py_DECREF(module_name); Py_DECREF(module); return NULL; } PyObject *actual = getattribute(module, dotted_path); Py_DECREF(module); if (actual == NULL) { + Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: attribute lookup %S on %S failed", global, global_name, module_name); @@ -1978,6 +1981,7 @@ whichmodule(PickleState *st, PyObject *global, PyObject *global_name, PyObject * } if (actual != global) { Py_DECREF(actual); + Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: it's not the same object as %S.%S", global, module_name, global_name); From cd0f8bfe57bd9f6c1a2ca6dea97bc4866d157626 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 5 Aug 2024 22:44:17 +0300 Subject: [PATCH 2/2] Move decref right after the PyErr_Format --- Modules/_pickle.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 5d6b5bb9bc5db6..dc0ef0a184d205 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1959,10 +1959,10 @@ whichmodule(PickleState *st, PyObject *global, PyObject *global_name, PyObject * extra parameters of __import__ to fix that. */ module = PyImport_Import(module_name); if (module == NULL) { - Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: import of module %R failed", global, module_name); + Py_DECREF(module_name); return NULL; } if (check_dotted_path(module, global_name, dotted_path) < 0) { @@ -1973,18 +1973,18 @@ whichmodule(PickleState *st, PyObject *global, PyObject *global_name, PyObject * PyObject *actual = getattribute(module, dotted_path); Py_DECREF(module); if (actual == NULL) { - Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: attribute lookup %S on %S failed", global, global_name, module_name); + Py_DECREF(module_name); return NULL; } if (actual != global) { Py_DECREF(actual); - Py_DECREF(module_name); PyErr_Format(st->PicklingError, "Can't pickle %R: it's not the same object as %S.%S", global, module_name, global_name); + Py_DECREF(module_name); return NULL; } Py_DECREF(actual);