From: Erlend E. Aasland Date: Sun, 11 Jun 2023 19:23:28 +0000 (+0200) Subject: gh-105375: Improve PyErr_WarnExplicit() error handling (#105610) X-Git-Tag: v3.13.0a1~1793 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=567d6ae8e77579173510fc948ac06b2ababf3d40;p=thirdparty%2FPython%2Fcpython.git gh-105375: Improve PyErr_WarnExplicit() error handling (#105610) Bail on first error to prevent exceptions from possibly being overwritten. --- diff --git a/Misc/NEWS.d/next/C API/2023-06-09-23-34-25.gh-issue-105375.n7amiF.rst b/Misc/NEWS.d/next/C API/2023-06-09-23-34-25.gh-issue-105375.n7amiF.rst new file mode 100644 index 000000000000..b9f95496f938 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-06-09-23-34-25.gh-issue-105375.n7amiF.rst @@ -0,0 +1,2 @@ +Fix a bug in :c:func:`PyErr_WarnExplicit` where an exception could end up +being overwritten if the API failed internally. diff --git a/Python/_warnings.c b/Python/_warnings.c index 69fa04e6befe..465cbf9bd303 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1298,25 +1298,29 @@ PyErr_WarnExplicit(PyObject *category, const char *text, const char *module_str, PyObject *registry) { PyObject *message = PyUnicode_FromString(text); + if (message == NULL) { + return -1; + } PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); + if (filename == NULL) { + Py_DECREF(message); + return -1; + } PyObject *module = NULL; - int ret = -1; - - if (message == NULL || filename == NULL) - goto exit; if (module_str != NULL) { module = PyUnicode_FromString(module_str); - if (module == NULL) - goto exit; + if (module == NULL) { + Py_DECREF(filename); + Py_DECREF(message); + return -1; + } } - ret = PyErr_WarnExplicitObject(category, message, filename, lineno, - module, registry); - - exit: - Py_XDECREF(message); + int ret = PyErr_WarnExplicitObject(category, message, filename, lineno, + module, registry); Py_XDECREF(module); - Py_XDECREF(filename); + Py_DECREF(filename); + Py_DECREF(message); return ret; }