From a0ae9ff00653f461f44a5b195654cc9dcd0fef92 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 22 Nov 2015 12:31:11 +0200 Subject: [PATCH] Issue #19687: Fixed memory leak on failed Element slice assignment. --- Modules/_elementtree.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 837164cd4b..48bf7800c6 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1390,15 +1390,17 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) if (step != 1 && newlen != slicelen) { + Py_XDECREF(seq); PyErr_Format(PyExc_ValueError, #if (PY_VERSION_HEX < 0x02050000) "attempt to assign sequence of size %d " "to extended slice of size %d", + (int)newlen, (int)slicelen #else "attempt to assign sequence of size %zd " "to extended slice of size %zd", -#endif newlen, slicelen +#endif ); return -1; } @@ -1407,9 +1409,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) /* Resize before creating the recycle bin, to prevent refleaks. */ if (newlen > slicelen) { if (element_resize(self, newlen - slicelen) < 0) { - if (seq) { - Py_DECREF(seq); - } + Py_XDECREF(seq); return -1; } } @@ -1420,9 +1420,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) we're done modifying the element */ recycle = PyList_New(slicelen); if (!recycle) { - if (seq) { - Py_DECREF(seq); - } + Py_XDECREF(seq); return -1; } for (cur = start, i = 0; i < slicelen; @@ -1450,9 +1448,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) self->extra->length += newlen - slicelen; - if (seq) { - Py_DECREF(seq); - } + Py_XDECREF(seq); /* discard the recycle bin, and everything in it */ Py_XDECREF(recycle); -- 2.50.1