Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1b9ee8

Browse files
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. (cherry picked from commit 9f3ed3e) Co-authored-by: Zackery Spytz <[email protected]>
1 parent 69d0372 commit b1b9ee8

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

Modules/_elementtree.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ static PyObject*
336336
get_attrib_from_keywords(PyObject *kwds)
337337
{
338338
PyObject *attrib_str = PyUnicode_FromString("attrib");
339+
if (attrib_str == NULL) {
340+
return NULL;
341+
}
339342
PyObject *attrib = PyDict_GetItem(kwds, attrib_str);
340343

341344
if (attrib) {
@@ -356,10 +359,10 @@ get_attrib_from_keywords(PyObject *kwds)
356359

357360
Py_DECREF(attrib_str);
358361

359-
/* attrib can be NULL if PyDict_New failed */
360-
if (attrib)
361-
if (PyDict_Update(attrib, kwds) < 0)
362-
return NULL;
362+
if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) {
363+
Py_DECREF(attrib);
364+
return NULL;
365+
}
363366
return attrib;
364367
}
365368

@@ -579,10 +582,9 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds)
579582
attrib = PyDict_Copy(attrib);
580583
if (!attrib)
581584
return NULL;
582-
if (kwds) {
583-
if (PyDict_Update(attrib, kwds) < 0) {
584-
return NULL;
585-
}
585+
if (kwds != NULL && PyDict_Update(attrib, kwds) < 0) {
586+
Py_DECREF(attrib);
587+
return NULL;
586588
}
587589
} else if (kwds) {
588590
/* have keyword args */
@@ -1828,7 +1830,6 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
18281830
* scheduled for removal.
18291831
*/
18301832
if (!(recycle = PyList_New(slicelen))) {
1831-
PyErr_NoMemory();
18321833
return -1;
18331834
}
18341835

@@ -1868,7 +1869,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
18681869
self->extra->length -= slicelen;
18691870

18701871
/* Discard the recycle list with all the deleted sub-elements */
1871-
Py_XDECREF(recycle);
1872+
Py_DECREF(recycle);
18721873
return 0;
18731874
}
18741875

@@ -3308,7 +3309,6 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
33083309
if (!target) {
33093310
Py_CLEAR(self->entity);
33103311
Py_CLEAR(self->names);
3311-
EXPAT(ParserFree)(self->parser);
33123312
return -1;
33133313
}
33143314
}

0 commit comments

Comments
 (0)