diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-25-14.gh-issue-105375.95g1eI.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-25-14.gh-issue-105375.95g1eI.rst new file mode 100644 index 00000000000000..1894b2b94bb334 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-25-14.gh-issue-105375.95g1eI.rst @@ -0,0 +1 @@ +Fix bugs in :mod:`!_elementtree` where exceptions could be overwritten. diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 00d9f647ccfdf3..6244fcc2064c33 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -3261,10 +3261,14 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, } while (attrib_in[0] && attrib_in[1]) { PyObject* key = makeuniversal(self, attrib_in[0]); + if (key == NULL) { + Py_DECREF(attrib); + Py_DECREF(tag); + return; + } PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); - if (!key || !value) { - Py_XDECREF(value); - Py_XDECREF(key); + if (value == NULL) { + Py_DECREF(key); Py_DECREF(attrib); Py_DECREF(tag); return;