Commit 9f3ed3e2 authored by Zackery Spytz's avatar Zackery Spytz Committed by Serhiy Storchaka

Fix error handling bugs in _elementtree.c. (GH-10060)

References could leak, NULL could be dereferenced, and the Expat parser could
be double freed when some errors raised.
parent 82af0b63
...@@ -336,6 +336,9 @@ static PyObject* ...@@ -336,6 +336,9 @@ static PyObject*
get_attrib_from_keywords(PyObject *kwds) get_attrib_from_keywords(PyObject *kwds)
{ {
PyObject *attrib_str = PyUnicode_FromString("attrib"); PyObject *attrib_str = PyUnicode_FromString("attrib");
if (attrib_str == NULL) {
return NULL;
}
PyObject *attrib = PyDict_GetItem(kwds, attrib_str); PyObject *attrib = PyDict_GetItem(kwds, attrib_str);
if (attrib) { if (attrib) {
...@@ -356,10 +359,10 @@ get_attrib_from_keywords(PyObject *kwds) ...@@ -356,10 +359,10 @@ get_attrib_from_keywords(PyObject *kwds)
Py_DECREF(attrib_str); Py_DECREF(attrib_str);
/* attrib can be NULL if PyDict_New failed */ if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) {
if (attrib) Py_DECREF(attrib);
if (PyDict_Update(attrib, kwds) < 0)
return NULL; return NULL;
}
return attrib; return attrib;
} }
...@@ -592,11 +595,10 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds) ...@@ -592,11 +595,10 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds)
attrib = PyDict_Copy(attrib); attrib = PyDict_Copy(attrib);
if (!attrib) if (!attrib)
return NULL; return NULL;
if (kwds) { if (kwds != NULL && PyDict_Update(attrib, kwds) < 0) {
if (PyDict_Update(attrib, kwds) < 0) { Py_DECREF(attrib);
return NULL; return NULL;
} }
}
} else if (kwds) { } else if (kwds) {
/* have keyword args */ /* have keyword args */
attrib = get_attrib_from_keywords(kwds); attrib = get_attrib_from_keywords(kwds);
...@@ -1871,7 +1873,6 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) ...@@ -1871,7 +1873,6 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
* scheduled for removal. * scheduled for removal.
*/ */
if (!(recycle = PyList_New(slicelen))) { if (!(recycle = PyList_New(slicelen))) {
PyErr_NoMemory();
return -1; return -1;
} }
...@@ -1911,7 +1912,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) ...@@ -1911,7 +1912,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
self->extra->length -= slicelen; self->extra->length -= slicelen;
/* Discard the recycle list with all the deleted sub-elements */ /* Discard the recycle list with all the deleted sub-elements */
Py_XDECREF(recycle); Py_DECREF(recycle);
return 0; return 0;
} }
...@@ -3338,7 +3339,6 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target, ...@@ -3338,7 +3339,6 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
if (!target) { if (!target) {
Py_CLEAR(self->entity); Py_CLEAR(self->entity);
Py_CLEAR(self->names); Py_CLEAR(self->names);
EXPAT(ParserFree)(self->parser);
return -1; return -1;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment