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

Skip to content

Commit d917dcb

Browse files
committed
Issue #18408: Fix constructors of _elementtree.c
* Use Py_DECREF() instead of PyObject_GC_Del() to release correctly all resources * Raise MemoryError on memory allocation failure
1 parent 81aac73 commit d917dcb

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

Modules/_elementtree.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,16 @@ create_new_element(PyObject* tag, PyObject* attrib)
237237

238238
self->weakreflist = NULL;
239239

240+
ALLOC(sizeof(ElementObject), "create element");
241+
PyObject_GC_Track(self);
242+
240243
if (attrib != Py_None && !is_empty_dict(attrib)) {
241244
if (create_extra(self, attrib) < 0) {
242-
PyObject_GC_Del(self);
245+
Py_DECREF(self);
243246
return NULL;
244247
}
245248
}
246249

247-
ALLOC(sizeof(ElementObject), "create element");
248-
PyObject_GC_Track(self);
249250
return (PyObject*) self;
250251
}
251252

@@ -2122,14 +2123,6 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext)
21222123
it = PyObject_GC_New(ElementIterObject, &ElementIter_Type);
21232124
if (!it)
21242125
return NULL;
2125-
if (!(it->parent_stack = PyObject_Malloc(sizeof(ParentLocator)))) {
2126-
PyObject_GC_Del(it);
2127-
return NULL;
2128-
}
2129-
2130-
it->parent_stack->parent = NULL;
2131-
it->parent_stack->child_index = 0;
2132-
it->parent_stack->next = NULL;
21332126

21342127
if (PyUnicode_Check(tag))
21352128
star = PyUnicode_FromString("*");
@@ -2147,8 +2140,18 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext)
21472140
Py_INCREF(self);
21482141
it->root_element = self;
21492142

2150-
21512143
PyObject_GC_Track(it);
2144+
2145+
it->parent_stack = PyObject_Malloc(sizeof(ParentLocator));
2146+
if (it->parent_stack == NULL) {
2147+
Py_DECREF(it);
2148+
PyErr_NoMemory();
2149+
return NULL;
2150+
}
2151+
it->parent_stack->parent = NULL;
2152+
it->parent_stack->child_index = 0;
2153+
it->parent_stack->next = NULL;
2154+
21522155
return (PyObject *)it;
21532156
}
21542157

0 commit comments

Comments
 (0)