From 74cb806f137ca606ef234305bea3ff744179ed28 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:25:43 +0200 Subject: [PATCH 1/3] gh-105375: Improve error handling in _elementtree Fix bugs where exceptions could be overwritten. --- .../2023-06-09-21-25-14.gh-issue-105375.95g1eI.rst | 1 + Modules/_elementtree.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-09-21-25-14.gh-issue-105375.95g1eI.rst 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 1e9c8bc4611850..b50d161e5d8e43 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -3259,10 +3259,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]); - PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); - if (!key || !value) { - Py_XDECREF(value); - Py_XDECREF(key); + if (key == NULL) { + Py_DECREF(attrib); + Py_DECREF(tag); + return + } + PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]) "strict"); + if (value == NULL) { + Py_DECREF(key); Py_DECREF(attrib); Py_DECREF(tag); return; From 7cf403a76c27cafa2bbaf4fd9f4b1f6021de29d3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:34:09 +0200 Subject: [PATCH 2/3] Revert accidental change --- Modules/_elementtree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index b50d161e5d8e43..486f59e9f8e730 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -3264,7 +3264,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, Py_DECREF(tag); return } - PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]) "strict"); + PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); if (value == NULL) { Py_DECREF(key); Py_DECREF(attrib); From 9b4fa510b422424997a5a4d1d5ceed89d99a324a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 9 Jun 2023 21:49:23 +0200 Subject: [PATCH 3/3] Missing semicolon --- Modules/_elementtree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 486f59e9f8e730..21a6e4e5b20264 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -3262,7 +3262,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, if (key == NULL) { Py_DECREF(attrib); Py_DECREF(tag); - return + return; } PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); if (value == NULL) {