Skip to content

Commit 2e6f51d

Browse files
committed
Handle errors and encoding
1 parent c6ab4f6 commit 2e6f51d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

ext/dom/document.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ PHP_METHOD(DOMDocument, saveXML)
15721572
libxml_doc_props const* doc_props = dom_get_doc_props_read_only(intern->document);
15731573
format = doc_props->formatoutput;
15741574

1575-
int status;
1575+
int status = -1;
15761576
if (nodep != NULL) {
15771577
/* Dump contents of Node */
15781578
DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
@@ -1590,13 +1590,18 @@ PHP_METHOD(DOMDocument, saveXML)
15901590
old_xml_save_no_empty_tags = xmlSaveNoEmptyTags;
15911591
xmlSaveNoEmptyTags = (options & LIBXML_SAVE_NOEMPTYTAG) ? 1 : 0;
15921592
if (php_dom_follow_spec_intern(intern)) {
1593-
// TODO: dedup
15941593
xmlSaveCtxtPtr ctxt = xmlSaveToBuffer(buf, (const char *) docp->encoding, XML_SAVE_AS_XML);
1595-
xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(buf, NULL); // TODO: set handler instead of NULL, & check return value
1596-
status = dom_xml_serialize(ctxt, out, node, format);
1597-
status |= xmlOutputBufferFlush(out);
1598-
status |= xmlOutputBufferClose(out);
1599-
(void) xmlSaveClose(ctxt);
1594+
if (EXPECTED(ctxt != NULL)) {
1595+
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler((const char *) docp->encoding);
1596+
xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(buf, handler);
1597+
if (EXPECTED(out != NULL)) {
1598+
status = dom_xml_serialize(ctxt, out, node, format);
1599+
status |= xmlOutputBufferFlush(out);
1600+
status |= xmlOutputBufferClose(out);
1601+
}
1602+
(void) xmlSaveClose(ctxt);
1603+
xmlCharEncCloseFunc(handler);
1604+
}
16001605
} else {
16011606
status = xmlNodeDump(buf, docp, node, 0, format);
16021607
}
@@ -1627,10 +1632,14 @@ PHP_METHOD(DOMDocument, saveXML)
16271632
RETURN_FALSE;
16281633
}
16291634
if (php_dom_follow_spec_intern(intern)) {
1630-
xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(buf, NULL); // TODO: set handler instead of NULL, & check return value
1631-
status = dom_xml_serialize(ctxt, out, (xmlNodePtr) docp, format);
1632-
status |= xmlOutputBufferFlush(out);
1633-
status |= xmlOutputBufferClose(out);
1635+
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler((const char *) docp->encoding);
1636+
xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(buf, handler);
1637+
if (EXPECTED(out != NULL)) {
1638+
status = dom_xml_serialize(ctxt, out, (xmlNodePtr) docp, format);
1639+
status |= xmlOutputBufferFlush(out);
1640+
status |= xmlOutputBufferClose(out);
1641+
}
1642+
xmlCharEncCloseFunc(handler);
16341643
} else {
16351644
status = xmlSaveDoc(ctxt, docp);
16361645
}

0 commit comments

Comments
 (0)