]> granicus.if.org Git - python/commitdiff
bpo-31544: Fix a reference leak to 'self' after the previous target error handling...
authorscoder <stefan_ml@behnel.de>
Sat, 31 Mar 2018 12:23:30 +0000 (14:23 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 31 Mar 2018 12:23:30 +0000 (15:23 +0300)
This change generally splits the xmlparser creation code into an unsafe part with "rollback" error handling and a safe "object initialisation done" part with normal decref cleanup.

Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31455.beTh6t.rst [new file with mode: 0644]
Modules/_elementtree.c

diff --git a/Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31455.beTh6t.rst b/Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31455.beTh6t.rst
new file mode 100644 (file)
index 0000000..9ea3599
--- /dev/null
@@ -0,0 +1,2 @@
+The C accelerator module of ElementTree ignored exceptions raised when
+looking up TreeBuilder target methods in XMLParser().
index 7f0e609340033a57b2b7b2398ffc8c2163cd3cd9..1d316a1c91d24b0836a436b616a74fb2ab12ca8c 100644 (file)
@@ -2575,14 +2575,24 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
         return NULL;
     }
 
+    ALLOC(sizeof(XMLParserObject), "create expatparser");
+
+    /* Init to NULL to keep the error handling below manageable. */
+    self->target =
+        self->handle_xml =
+        self->handle_start =
+        self->handle_data =
+        self->handle_end =
+        self->handle_comment =
+        self->handle_pi =
+        self->handle_close =
+        NULL;
+
     /* setup target handlers */
     if (!target) {
         target = treebuilder_new();
         if (!target) {
-            EXPAT(ParserFree)(self->parser);
-            PyObject_Del(self->names);
-            PyObject_Del(self->entity);
-            PyObject_Del(self);
+            Py_DECREF(self);
             return NULL;
         }
     } else
@@ -2591,30 +2601,37 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
 
     self->handle_xml = PyObject_GetAttrString(target, "xml");
     if (ignore_attribute_error(self->handle_xml)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_start = PyObject_GetAttrString(target, "start");
     if (ignore_attribute_error(self->handle_start)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_data = PyObject_GetAttrString(target, "data");
     if (ignore_attribute_error(self->handle_data)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_end = PyObject_GetAttrString(target, "end");
     if (ignore_attribute_error(self->handle_end)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_comment = PyObject_GetAttrString(target, "comment");
     if (ignore_attribute_error(self->handle_comment)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_pi = PyObject_GetAttrString(target, "pi");
     if (ignore_attribute_error(self->handle_pi)) {
+        Py_DECREF(self);
         return NULL;
     }
     self->handle_close = PyObject_GetAttrString(target, "close");
     if (ignore_attribute_error(self->handle_close)) {
+        Py_DECREF(self);
         return NULL;
     }
 
@@ -2650,8 +2667,6 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
         );
 #endif
 
-    ALLOC(sizeof(XMLParserObject), "create expatparser");
-
     return (PyObject*) self;
 }