Skip to content

Commit

Permalink
Resolve crash when serializing lots of XML documents
Browse files Browse the repository at this point in the history
This should resolve
ximion/appstream-generator#46
  • Loading branch information
ximion committed Jul 10, 2017
1 parent ad4b83e commit f68b511
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
12 changes: 2 additions & 10 deletions src/as-metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ as_metadata_component_to_metainfo (AsMetadata *metad, AsFormatKind format, GErro
return NULL;

node = as_component_to_xml_node (cpt, context, NULL);
xmlstr = as_xml_node_to_str (node);
xmlstr = as_xml_node_to_str (node, error);

return xmlstr;
}
Expand All @@ -831,9 +831,7 @@ static gchar*
as_metadata_xml_serialize_to_collection_with_rootnode (AsMetadata *metad, AsContext *context, GPtrArray *cpts)
{
AsMetadataPrivate *priv = GET_PRIVATE (metad);
xmlDoc *doc;
xmlNode *root;
gchar *xmlstr = NULL;
guint i;

root = xmlNewNode (NULL, (xmlChar*) "components");
Expand All @@ -855,13 +853,7 @@ as_metadata_xml_serialize_to_collection_with_rootnode (AsMetadata *metad, AsCont
xmlAddChild (root, node);
}

doc = xmlNewDoc ((xmlChar*) NULL);
xmlDocSetRootElement (doc, root);

xmlDocDumpFormatMemoryEnc (doc, (xmlChar**) (&xmlstr), NULL, "utf-8", TRUE);
xmlFreeDoc (doc);

return xmlstr;
return as_xml_node_to_str (root, NULL);
}

/**
Expand Down
23 changes: 20 additions & 3 deletions src/as-xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ as_xml_parse_document (const gchar *data, GError **error)
as_xml_set_out_of_context_error (NULL);
return NULL;
}
as_xml_set_out_of_context_error (NULL);

root = xmlDocGetRootElement (doc);
if (root == NULL) {
Expand All @@ -590,19 +591,35 @@ as_xml_parse_document (const gchar *data, GError **error)
* Returns: XML metadata.
*/
gchar*
as_xml_node_to_str (xmlNode *root)
as_xml_node_to_str (xmlNode *root, GError **error)
{
xmlDoc *doc;
gchar *xmlstr = NULL;
g_autofree gchar *error_msg_str = NULL;

as_xml_set_out_of_context_error (&error_msg_str);
doc = xmlNewDoc ((xmlChar*) NULL);
if (root == NULL)
goto out;

xmlDocSetRootElement (doc, root);
xmlDocDumpFormatMemoryEnc (doc, (xmlChar**) (&xmlstr), NULL, "utf-8", TRUE);

if (error_msg_str != NULL) {
if (error == NULL) {
g_warning ("Could not serialize XML document: %s", error_msg_str);
goto out;
} else {
g_set_error (error,
AS_METADATA_ERROR,
AS_METADATA_ERROR_FAILED,
"Could not serialize XML document: %s", error_msg_str);
goto out;
}
}

out:
xmlDocDumpFormatMemoryEnc (doc, (xmlChar**) (&xmlstr), NULL, "utf-8", TRUE);
as_xml_set_out_of_context_error (NULL);
xmlFreeDoc (doc);

return xmlstr;
}
2 changes: 1 addition & 1 deletion src/as-xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ xmlNode *as_xml_add_text_node (xmlNode *root,
xmlDoc *as_xml_parse_document (const gchar *data,
GError **error);

gchar *as_xml_node_to_str (xmlNode *root);
gchar *as_xml_node_to_str (xmlNode *root, GError **error);

#pragma GCC visibility pop
G_END_DECLS
Expand Down

0 comments on commit f68b511

Please sign in to comment.