From eede1d2f48b4fe7f7918952d9ebeb744b58668c1 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:57:25 +0200 Subject: [PATCH] gh-105375: Improve errnomodule error handling (#105590) Bail immediately if an exception is set, to prevent exceptions from being overwritten. --- .../2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst | 1 + Modules/errnomodule.c | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst new file mode 100644 index 00000000000..3030477c824 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst @@ -0,0 +1 @@ +Fix bugs in :mod:`pickle` where exceptions could be overwritten. diff --git a/Modules/errnomodule.c b/Modules/errnomodule.c index 3d0c2d7ae94..301ad8313bc 100644 --- a/Modules/errnomodule.c +++ b/Modules/errnomodule.c @@ -81,10 +81,12 @@ end: static int errno_exec(PyObject *module) { - PyObject *module_dict = PyModule_GetDict(module); + PyObject *module_dict = PyModule_GetDict(module); // Borrowed ref. + if (module_dict == NULL) { + return -1; + } PyObject *error_dict = PyDict_New(); - if (!module_dict || !error_dict) { - Py_XDECREF(error_dict); + if (error_dict == NULL) { return -1; } if (PyDict_SetItemString(module_dict, "errorcode", error_dict) < 0) {