@@ -1586,47 +1586,6 @@ PHP_METHOD(DOMDocument, saveXML)
15861586}
15871587/* }}} end dom_document_savexml */
15881588
1589- static xmlNodePtr php_dom_free_xinclude_node (xmlNodePtr cur ) /* {{{ */
1590- {
1591- xmlNodePtr xincnode ;
1592-
1593- xincnode = cur ;
1594- cur = cur -> next ;
1595- xmlUnlinkNode (xincnode );
1596- php_libxml_node_free_resource (xincnode );
1597-
1598- return cur ;
1599- }
1600- /* }}} */
1601-
1602- static void php_dom_remove_xinclude_nodes (xmlNodePtr cur ) /* {{{ */
1603- {
1604- while (cur ) {
1605- if (cur -> type == XML_XINCLUDE_START ) {
1606- cur = php_dom_free_xinclude_node (cur );
1607-
1608- /* XML_XINCLUDE_END node will be a sibling of XML_XINCLUDE_START */
1609- while (cur && cur -> type != XML_XINCLUDE_END ) {
1610- /* remove xinclude processing nodes from recursive xincludes */
1611- if (cur -> type == XML_ELEMENT_NODE ) {
1612- php_dom_remove_xinclude_nodes (cur -> children );
1613- }
1614- cur = cur -> next ;
1615- }
1616-
1617- if (cur && cur -> type == XML_XINCLUDE_END ) {
1618- cur = php_dom_free_xinclude_node (cur );
1619- }
1620- } else {
1621- if (cur -> type == XML_ELEMENT_NODE ) {
1622- php_dom_remove_xinclude_nodes (cur -> children );
1623- }
1624- cur = cur -> next ;
1625- }
1626- }
1627- }
1628- /* }}} */
1629-
16301589/* Backported from master branch xml_common.h */
16311590static zend_always_inline xmlNodePtr php_dom_next_in_tree_order (const xmlNode * nodep , const xmlNode * basep )
16321591{
@@ -1660,17 +1619,19 @@ static void dom_xinclude_strip_references(xmlNodePtr basep)
16601619 }
16611620}
16621621
1663- /* See GH-14702.
1664- * We have to remove userland references to xinclude fallback nodes because libxml2 will make clones of these
1622+ /* See GH-14702 and GH-17847 .
1623+ * We have to remove userland references to xinclude nodes because libxml2 will make clones of these
16651624 * and remove the original nodes. If the originals are removed while there are still userland references
16661625 * this will cause memory corruption. */
16671626static void dom_xinclude_strip_fallback_references (const xmlNode * basep )
16681627{
16691628 xmlNodePtr current = basep -> children ;
16701629
1630+ /* TODO: try to improve loop search performance */
16711631 while (current ) {
1672- if (current -> type == XML_ELEMENT_NODE && current -> ns != NULL && current -> _private != NULL
1673- && xmlStrEqual (current -> name , XINCLUDE_FALLBACK )
1632+ if (current -> type == XML_ELEMENT_NODE
1633+ && current -> ns != NULL
1634+ && xmlStrEqual (current -> name , XINCLUDE_NODE )
16741635 && (xmlStrEqual (current -> ns -> href , XINCLUDE_NS ) || xmlStrEqual (current -> ns -> href , XINCLUDE_OLD_NS ))) {
16751636 dom_xinclude_strip_references (current );
16761637 }
@@ -1684,7 +1645,6 @@ PHP_METHOD(DOMDocument, xinclude)
16841645{
16851646 zval * id ;
16861647 xmlDoc * docp ;
1687- xmlNodePtr root ;
16881648 zend_long flags = 0 ;
16891649 int err ;
16901650 dom_object * intern ;
@@ -1703,22 +1663,11 @@ PHP_METHOD(DOMDocument, xinclude)
17031663
17041664 dom_xinclude_strip_fallback_references ((const xmlNode * ) docp );
17051665
1666+ flags |= XML_PARSE_NOXINCNODE ;
17061667 PHP_LIBXML_SANITIZE_GLOBALS (xinclude );
17071668 err = xmlXIncludeProcessFlags (docp , (int )flags );
17081669 PHP_LIBXML_RESTORE_GLOBALS (xinclude );
17091670
1710- /* XML_XINCLUDE_START and XML_XINCLUDE_END nodes need to be removed as these
1711- are added via xmlXIncludeProcess to mark beginning and ending of xincluded document
1712- but are not wanted in resulting document - must be done even if err as it could fail after
1713- having processed some xincludes */
1714- root = (xmlNodePtr ) docp -> children ;
1715- while (root && root -> type != XML_ELEMENT_NODE && root -> type != XML_XINCLUDE_START ) {
1716- root = root -> next ;
1717- }
1718- if (root ) {
1719- php_dom_remove_xinclude_nodes (root );
1720- }
1721-
17221671 php_libxml_invalidate_node_list_cache (intern -> document );
17231672
17241673 if (err ) {
0 commit comments