Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31455: fix reference leak #6318

Merged
merged 4 commits into from Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
The C accelerator module of ElementTree ignored exceptions raised when
looking up TreeBuilder target methods in XMLParser().
27 changes: 21 additions & 6 deletions Modules/_elementtree.c
Expand Up @@ -2575,14 +2575,24 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
return NULL;
}

ALLOC(sizeof(XMLParserObject), "create expatparser");

/* Init to NULL to keep the error handling below manageable. */
self->target =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed. PyObject_New() clears the memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so, too, until the test started crashing due to uninitalised struct fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, seems I remember this wrong.

self->handle_xml =
self->handle_start =
self->handle_data =
self->handle_end =
self->handle_comment =
self->handle_pi =
self->handle_close =
NULL;

/* setup target handlers */
if (!target) {
target = treebuilder_new();
if (!target) {
EXPAT(ParserFree)(self->parser);
PyObject_Del(self->names);
PyObject_Del(self->entity);
PyObject_Del(self);
Py_DECREF(self);
return NULL;
}
} else
Expand All @@ -2591,30 +2601,37 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)

self->handle_xml = PyObject_GetAttrString(target, "xml");
if (ignore_attribute_error(self->handle_xml)) {
Py_DECREF(self);
return NULL;
}
self->handle_start = PyObject_GetAttrString(target, "start");
if (ignore_attribute_error(self->handle_start)) {
Py_DECREF(self);
return NULL;
}
self->handle_data = PyObject_GetAttrString(target, "data");
if (ignore_attribute_error(self->handle_data)) {
Py_DECREF(self);
return NULL;
}
self->handle_end = PyObject_GetAttrString(target, "end");
if (ignore_attribute_error(self->handle_end)) {
Py_DECREF(self);
return NULL;
}
self->handle_comment = PyObject_GetAttrString(target, "comment");
if (ignore_attribute_error(self->handle_comment)) {
Py_DECREF(self);
return NULL;
}
self->handle_pi = PyObject_GetAttrString(target, "pi");
if (ignore_attribute_error(self->handle_pi)) {
Py_DECREF(self);
return NULL;
}
self->handle_close = PyObject_GetAttrString(target, "close");
if (ignore_attribute_error(self->handle_close)) {
Py_DECREF(self);
return NULL;
}

Expand Down Expand Up @@ -2650,8 +2667,6 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
);
#endif

ALLOC(sizeof(XMLParserObject), "create expatparser");

return (PyObject*) self;
}

Expand Down