diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-22 10:18:38 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-22 10:18:38 (GMT) |
commit | 04d759b1e4cb5b2d9ed13555e952565ccd3ce3bd (patch) | |
tree | a65945eb36aa1f276ce012de129055d7a216d001 /Modules | |
parent | 828123ce4e4d9f5a98ad4912817e7e86e1deae0c (diff) | |
download | cpython-04d759b1e4cb5b2d9ed13555e952565ccd3ce3bd.zip cpython-04d759b1e4cb5b2d9ed13555e952565ccd3ce3bd.tar.gz cpython-04d759b1e4cb5b2d9ed13555e952565ccd3ce3bd.tar.bz2 |
Issue #19687: Fixed memory leak on failed Element slice assignment.
Added new tests for Element slice assignments.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_elementtree.c | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 826342a..da784a0 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1605,7 +1605,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) Py_ssize_t start, stop, step, slicelen, newlen, cur, i; PyObject* recycle = NULL; - PyObject* seq = NULL; + PyObject* seq; if (!self->extra) { if (create_extra(self, NULL) < 0) @@ -1684,21 +1684,21 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) Py_XDECREF(recycle); return 0; } - else { - /* A new slice is actually being assigned */ - seq = PySequence_Fast(value, ""); - if (!seq) { - PyErr_Format( - PyExc_TypeError, - "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name - ); - return -1; - } - newlen = PySequence_Size(seq); + + /* A new slice is actually being assigned */ + seq = PySequence_Fast(value, ""); + if (!seq) { + PyErr_Format( + PyExc_TypeError, + "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name + ); + return -1; } + newlen = PySequence_Size(seq); if (step != 1 && newlen != slicelen) { + Py_DECREF(seq); PyErr_Format(PyExc_ValueError, "attempt to assign sequence of size %zd " "to extended slice of size %zd", @@ -1710,9 +1710,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_DECREF(seq); return -1; } } @@ -1723,9 +1721,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_DECREF(seq); return -1; } for (cur = start, i = 0; i < slicelen; @@ -1753,9 +1749,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) self->extra->length += newlen - slicelen; - if (seq) { - Py_DECREF(seq); - } + Py_DECREF(seq); /* discard the recycle bin, and everything in it */ Py_XDECREF(recycle); |