From 424c2654780b7d216abfc5eef37b8d73f1fe5df4 Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 10 Sep 2021 09:14:18 +0200 Subject: [PATCH] Fix #81433: DOMElement::setIdAttribute() called twice may remove ID We must only remove the attribute id, if the user requested that. Closes GH-7482. --- NEWS | 3 +++ ext/dom/element.c | 8 +++----- ext/dom/tests/bug81433.phpt | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 ext/dom/tests/bug81433.phpt diff --git a/NEWS b/NEWS index c955c514a6c..86943c9c7f6 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 7.4.25 +- DOM: + . Fixed bug #81433 (DOMElement::setIdAttribute() called twice may remove ID). + (Viktor Volkov) 23 Dep 2021, PHP 7.4.24 diff --git a/ext/dom/element.c b/ext/dom/element.c index 51aba0e1551..274b11cfe1b 100644 --- a/ext/dom/element.c +++ b/ext/dom/element.c @@ -1157,11 +1157,9 @@ static void php_set_attribute_id(xmlAttrPtr attrp, zend_bool is_id) /* {{{ */ xmlAddID(NULL, attrp->doc, id_val, attrp); xmlFree(id_val); } - } else { - if (attrp->atype == XML_ATTRIBUTE_ID) { - xmlRemoveID(attrp->doc, attrp); - attrp->atype = 0; - } + } else if (is_id == 0 && attrp->atype == XML_ATTRIBUTE_ID) { + xmlRemoveID(attrp->doc, attrp); + attrp->atype = 0; } } /* }}} */ diff --git a/ext/dom/tests/bug81433.phpt b/ext/dom/tests/bug81433.phpt new file mode 100644 index 00000000000..d587fe4de56 --- /dev/null +++ b/ext/dom/tests/bug81433.phpt @@ -0,0 +1,26 @@ +--TEST-- +Bug #81433 (DOMElement::setIdAttribute(attr, true) called twice removes ID) +--SKIPIF-- + +--FILE-- +createElement('test', 'root'); + +$dom->appendChild($element); + +$element->setAttribute("id", 123); +$element->setIdAttribute("id", true); + +$node = $element->getAttributeNode("id"); +var_dump($node->isId()); + +$element->setIdAttribute("id", true); +var_dump($node->isId()); +?> +--EXPECT-- +bool(true) +bool(true)