Skip to content

Commit 88944a4

Browse files
bpo-39903: Fix double decref in _elementtree.Element.__getstate__ (GH-18850)
1 parent fc72ab6 commit 88944a4

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

Modules/_elementtree.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,8 @@ static PyObject *
950950
_elementtree_Element___getstate___impl(ElementObject *self)
951951
/*[clinic end generated code: output=37279aeeb6bb5b04 input=f0d16d7ec2f7adc1]*/
952952
{
953-
Py_ssize_t i, noattrib;
954-
PyObject *instancedict = NULL, *children;
953+
Py_ssize_t i;
954+
PyObject *children, *attrib;
955955

956956
/* Build a list of children. */
957957
children = PyList_New(self->extra ? self->extra->length : 0);
@@ -963,33 +963,24 @@ _elementtree_Element___getstate___impl(ElementObject *self)
963963
PyList_SET_ITEM(children, i, child);
964964
}
965965

966-
/* Construct the state object. */
967-
noattrib = (self->extra == NULL || self->extra->attrib == Py_None);
968-
if (noattrib)
969-
instancedict = Py_BuildValue("{sOsOs{}sOsO}",
970-
PICKLED_TAG, self->tag,
971-
PICKLED_CHILDREN, children,
972-
PICKLED_ATTRIB,
973-
PICKLED_TEXT, JOIN_OBJ(self->text),
974-
PICKLED_TAIL, JOIN_OBJ(self->tail));
975-
else
976-
instancedict = Py_BuildValue("{sOsOsOsOsO}",
977-
PICKLED_TAG, self->tag,
978-
PICKLED_CHILDREN, children,
979-
PICKLED_ATTRIB, self->extra->attrib,
980-
PICKLED_TEXT, JOIN_OBJ(self->text),
981-
PICKLED_TAIL, JOIN_OBJ(self->tail));
982-
if (instancedict) {
983-
Py_DECREF(children);
984-
return instancedict;
966+
if (self->extra && self->extra->attrib != Py_None) {
967+
attrib = self->extra->attrib;
968+
Py_INCREF(attrib);
985969
}
986970
else {
987-
for (i = 0; i < PyList_GET_SIZE(children); i++)
988-
Py_DECREF(PyList_GET_ITEM(children, i));
989-
Py_DECREF(children);
990-
991-
return NULL;
971+
attrib = PyDict_New();
972+
if (!attrib) {
973+
Py_DECREF(children);
974+
return NULL;
975+
}
992976
}
977+
978+
return Py_BuildValue("{sOsNsNsOsO}",
979+
PICKLED_TAG, self->tag,
980+
PICKLED_CHILDREN, children,
981+
PICKLED_ATTRIB, attrib,
982+
PICKLED_TEXT, JOIN_OBJ(self->text),
983+
PICKLED_TAIL, JOIN_OBJ(self->tail));
993984
}
994985

995986
static PyObject *

0 commit comments

Comments
 (0)