@@ -990,7 +990,7 @@ static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNode
990990PHP_METHOD (DOMNode , insertBefore )
991991{
992992 zval * id , * node , * ref = NULL ;
993- xmlNodePtr child , new_child , parentp , refp ;
993+ xmlNodePtr child , new_child , parentp , refp = NULL ;
994994 dom_object * intern , * childobj , * refpobj ;
995995 int ret , stricterror ;
996996
@@ -1015,19 +1015,21 @@ PHP_METHOD(DOMNode, insertBefore)
10151015 RETURN_FALSE ;
10161016 }
10171017
1018- if (child -> doc == NULL && parentp -> doc != NULL ) {
1019- dom_set_document_ref_pointers (child , intern -> document );
1020- }
1021-
1022- php_libxml_invalidate_node_list_cache (intern -> document );
1023-
10241018 if (ref != NULL ) {
10251019 DOM_GET_OBJ (refp , ref , xmlNodePtr , refpobj );
10261020 if (refp -> parent != parentp ) {
10271021 php_dom_throw_error (NOT_FOUND_ERR , stricterror );
10281022 RETURN_FALSE ;
10291023 }
1024+ }
1025+
1026+ if (child -> doc == NULL && parentp -> doc != NULL ) {
1027+ dom_set_document_ref_pointers (child , intern -> document );
1028+ }
10301029
1030+ php_libxml_invalidate_node_list_cache (intern -> document );
1031+
1032+ if (ref != NULL ) {
10311033 if (child -> parent != NULL ) {
10321034 xmlUnlinkNode (child );
10331035 }
@@ -1173,6 +1175,13 @@ PHP_METHOD(DOMNode, replaceChild)
11731175 RETURN_FALSE ;
11741176 }
11751177
1178+ /* This is already disallowed by libxml, but we should check it here to avoid
1179+ * breaking assumptions and assertions. */
1180+ if ((oldchild -> type == XML_ATTRIBUTE_NODE ) != (newchild -> type == XML_ATTRIBUTE_NODE )) {
1181+ php_dom_throw_error (HIERARCHY_REQUEST_ERR , stricterror );
1182+ RETURN_FALSE ;
1183+ }
1184+
11761185 if (oldchild -> parent != nodep ) {
11771186 php_dom_throw_error (NOT_FOUND_ERR , stricterror );
11781187 RETURN_FALSE ;
0 commit comments