@@ -1572,7 +1572,7 @@ PHP_METHOD(DOMDocument, saveXML)
1572
1572
libxml_doc_props const * doc_props = dom_get_doc_props_read_only (intern -> document );
1573
1573
format = doc_props -> formatoutput ;
1574
1574
1575
- int status ;
1575
+ int status = -1 ;
1576
1576
if (nodep != NULL ) {
1577
1577
/* Dump contents of Node */
1578
1578
DOM_GET_OBJ (node , nodep , xmlNodePtr , nodeobj );
@@ -1590,13 +1590,18 @@ PHP_METHOD(DOMDocument, saveXML)
1590
1590
old_xml_save_no_empty_tags = xmlSaveNoEmptyTags ;
1591
1591
xmlSaveNoEmptyTags = (options & LIBXML_SAVE_NOEMPTYTAG ) ? 1 : 0 ;
1592
1592
if (php_dom_follow_spec_intern (intern )) {
1593
- // TODO: dedup
1594
1593
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
+ }
1600
1605
} else {
1601
1606
status = xmlNodeDump (buf , docp , node , 0 , format );
1602
1607
}
@@ -1627,10 +1632,14 @@ PHP_METHOD(DOMDocument, saveXML)
1627
1632
RETURN_FALSE ;
1628
1633
}
1629
1634
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 );
1634
1643
} else {
1635
1644
status = xmlSaveDoc (ctxt , docp );
1636
1645
}
0 commit comments