diff --git a/NEWS b/NEWS index 11cf6cd0214..857248b413e 100644 --- a/NEWS +++ b/NEWS @@ -40,6 +40,7 @@ PHP NEWS an element). (nielsdos) . Fixed bug GH-16535 (UAF when using document as a child). (nielsdos) . Fixed bug GH-16593 (Assertion failure in DOM->replaceChild). (nielsdos) + . Fixed bug GH-16595 (Another UAF in DOM -> cloneNode). (nielsdos) - EXIF: . Fixed bug GH-16409 (Segfault in exif_thumbnail when not dealing with a diff --git a/ext/dom/node.c b/ext/dom/node.c index 188806564c6..fc668415dae 100644 --- a/ext/dom/node.c +++ b/ext/dom/node.c @@ -893,7 +893,7 @@ Since: PHP_METHOD(DOMNode, insertBefore) { zval *id, *node, *ref = NULL; - xmlNodePtr child, new_child, parentp, refp; + xmlNodePtr child, new_child, parentp, refp = NULL; dom_object *intern, *childobj, *refpobj; int ret, stricterror; @@ -918,18 +918,21 @@ PHP_METHOD(DOMNode, insertBefore) RETURN_FALSE; } - if (child->doc == NULL && parentp->doc != NULL) { - childobj->document = intern->document; - php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL); - } - + /* Fetch and perform sanity checks before modifying reference pointers. */ if (ref != NULL) { DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj); if (refp->parent != parentp) { php_dom_throw_error(NOT_FOUND_ERR, stricterror); RETURN_FALSE; } + } + if (child->doc == NULL && parentp->doc != NULL) { + childobj->document = intern->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL); + } + + if (ref != NULL) { if (child->parent != NULL) { xmlUnlinkNode(child); }