@@ -895,21 +895,23 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
895895 RETURN_FALSE ;
896896 }
897897
898- if (child -> doc == NULL && parentp -> doc != NULL ) {
899- dom_set_document_ref_pointers (child , intern -> document );
900- }
901-
902- php_libxml_invalidate_node_list_cache (intern -> document );
903-
898+ xmlNodePtr refp = NULL ;
904899 if (ref != NULL ) {
905- xmlNodePtr refp ;
906900 dom_object * refpobj ;
907901 DOM_GET_OBJ (refp , ref , xmlNodePtr , refpobj );
908902 if (refp -> parent != parentp ) {
909903 php_dom_throw_error (NOT_FOUND_ERR , stricterror );
910904 RETURN_FALSE ;
911905 }
906+ }
907+
908+ if (child -> doc == NULL && parentp -> doc != NULL ) {
909+ dom_set_document_ref_pointers (child , intern -> document );
910+ }
911+
912+ php_libxml_invalidate_node_list_cache (intern -> document );
912913
914+ if (ref != NULL ) {
913915 if (child -> parent != NULL ) {
914916 xmlUnlinkNode (child );
915917 }
@@ -1196,6 +1198,13 @@ static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
11961198 RETURN_FALSE ;
11971199 }
11981200
1201+ /* This is already disallowed by libxml, but we should check it here to avoid
1202+ * breaking assumptions and assertions. */
1203+ if ((oldchild -> type == XML_ATTRIBUTE_NODE ) != (newchild -> type == XML_ATTRIBUTE_NODE )) {
1204+ php_dom_throw_error (HIERARCHY_REQUEST_ERR , stricterror );
1205+ RETURN_FALSE ;
1206+ }
1207+
11991208 if (oldchild -> parent != nodep ) {
12001209 php_dom_throw_error (NOT_FOUND_ERR , stricterror );
12011210 RETURN_FALSE ;
0 commit comments