summaryrefslogtreecommitdiffstats
path: root/Modules/_elementtree.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-22 10:30:28 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-22 10:30:28 (GMT)
commit36e4f760f69a388b5a6097b2e9113dbd464dac90 (patch)
treea57f071c44d86666af665455abf7d41d0d8c2596 /Modules/_elementtree.c
parent23244e5787209254143753f8bbaecde7d29ea9da (diff)
parent04d759b1e4cb5b2d9ed13555e952565ccd3ce3bd (diff)
downloadcpython-36e4f760f69a388b5a6097b2e9113dbd464dac90.zip
cpython-36e4f760f69a388b5a6097b2e9113dbd464dac90.tar.gz
cpython-36e4f760f69a388b5a6097b2e9113dbd464dac90.tar.bz2
Issue #19687: Fixed memory leak on failed Element slice assignment.
Added new tests for Element slice assignments.
Diffstat (limited to 'Modules/_elementtree.c')
-rw-r--r--Modules/_elementtree.c36
1 files changed, 15 insertions, 21 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 911b5ac..02cd6d4 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1711,7 +1711,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)
@@ -1790,21 +1790,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",
@@ -1816,9 +1816,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;
}
}
@@ -1829,9 +1827,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;
@@ -1859,9 +1855,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);