Skip to content

Fix manually calling __construct() on DOM classes #11894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/dom/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PHP_METHOD(DOMAttr, __construct)

oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/cdatasection.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PHP_METHOD(DOMCdataSection, __construct)
intern = Z_DOMOBJ_P(ZEND_THIS);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
}
Expand Down
10 changes: 4 additions & 6 deletions ext/dom/comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ PHP_METHOD(DOMComment, __construct)
}

intern = Z_DOMOBJ_P(ZEND_THIS);
if (intern != NULL) {
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
}
/* }}} end DOMComment::__construct */

Expand Down
26 changes: 12 additions & 14 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,22 +1118,20 @@ PHP_METHOD(DOMDocument, __construct)
}

intern = Z_DOMOBJ_P(ZEND_THIS);
if (intern != NULL) {
olddoc = (xmlDocPtr) dom_object_get_node(intern);
if (olddoc != NULL) {
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
if (refcount != 0) {
olddoc->_private = NULL;
}
}
intern->document = NULL;
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, docp) == -1) {
/* docp is always non-null so php_libxml_increment_doc_ref() never returns -1 */
ZEND_UNREACHABLE();
olddoc = (xmlDocPtr) dom_object_get_node(intern);
if (olddoc != NULL) {
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
if (refcount != 0) {
olddoc->_private = NULL;
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)docp, (void *)intern);
}
intern->document = NULL;
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, docp) == -1) {
/* docp is always non-null so php_libxml_increment_doc_ref() never returns -1 */
ZEND_UNREACHABLE();
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)docp, (void *)intern);
}
/* }}} end DOMDocument::__construct */

Expand Down
3 changes: 1 addition & 2 deletions ext/dom/documentfragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ PHP_METHOD(DOMDocumentFragment, __construct)
intern = Z_DOMOBJ_P(ZEND_THIS);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
/* php_dom_set_object(intern, nodep); */
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
}
/* }}} end DOMDocumentFragment::__construct */
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ PHP_METHOD(DOMElement, __construct)
intern = Z_DOMOBJ_P(ZEND_THIS);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
}
Expand Down
10 changes: 4 additions & 6 deletions ext/dom/entityreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ PHP_METHOD(DOMEntityReference, __construct)
}

intern = Z_DOMOBJ_P(ZEND_THIS);
if (intern != NULL) {
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, node, (void *)intern);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, node, (void *)intern);
}
/* }}} end DOMEntityReference::__construct */

Expand Down
2 changes: 1 addition & 1 deletion ext/dom/processinginstruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PHP_METHOD(DOMProcessingInstruction, __construct)
intern = Z_DOMOBJ_P(ZEND_THIS);
oldnode = dom_object_get_node(intern);
if (oldnode != NULL) {
php_libxml_node_free_resource(oldnode );
php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
}
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
}
Expand Down
16 changes: 0 additions & 16 deletions ext/dom/tests/DOMDocumentFragment_construct_basic_002.phpt

This file was deleted.

34 changes: 34 additions & 0 deletions ext/dom/tests/manually_call_constructor/attribute.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Manually call __construct() - attribute variation
--EXTENSIONS--
dom
--FILE--
<?php

$attr = new DOMAttr("attribute", "my value");
var_dump($attr->nodeName, $attr->nodeValue);
$attr->__construct("newattribute", "my new value");
var_dump($attr->nodeName, $attr->nodeValue);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->setAttributeNode($attr);
echo $doc->saveXML();

$attr->__construct("newnewattribute", "my even newer value");
$doc->documentElement->setAttributeNode($attr);
echo $doc->saveXML();

?>
--EXPECT--
string(9) "attribute"
string(8) "my value"
string(12) "newattribute"
string(12) "my new value"
<?xml version="1.0"?>
<container newattribute="my new value"/>
<?xml version="1.0"?>
<container newattribute="my new value" newnewattribute="my even newer value"/>
32 changes: 32 additions & 0 deletions ext/dom/tests/manually_call_constructor/cdatasection.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Manually call __construct() - CDATA section variation
--EXTENSIONS--
dom
--FILE--
<?php

$cdata = new DOMCdataSection("my value");
var_dump($cdata->nodeValue);
$cdata->__construct("my new value");
var_dump($cdata->nodeValue);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($cdata);
echo $doc->saveXML();

$cdata->__construct("my even newer value");
$doc->documentElement->appendChild($cdata);
echo $doc->saveXML();

?>
--EXPECT--
string(8) "my value"
string(12) "my new value"
<?xml version="1.0"?>
<container><![CDATA[my new value]]></container>
<?xml version="1.0"?>
<container><![CDATA[my new value]]><![CDATA[my even newer value]]></container>
34 changes: 34 additions & 0 deletions ext/dom/tests/manually_call_constructor/comment.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Manually call __construct() - comment variation
--EXTENSIONS--
dom
--FILE--
<?php

$comment = new DOMComment("my value");
var_dump($comment->nodeName, $comment->nodeValue);
$comment->__construct("my new value");
var_dump($comment->nodeName, $comment->nodeValue);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($comment);
echo $doc->saveXML();

$comment->__construct("my even newer value");
$doc->documentElement->appendChild($comment);
echo $doc->saveXML();

?>
--EXPECT--
string(8) "#comment"
string(8) "my value"
string(8) "#comment"
string(12) "my new value"
<?xml version="1.0"?>
<container><!--my new value--></container>
<?xml version="1.0"?>
<container><!--my new value--><!--my even newer value--></container>
15 changes: 15 additions & 0 deletions ext/dom/tests/manually_call_constructor/document.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Manually call __construct() - document variation
--EXTENSIONS--
dom
--FILE--
<?php

$doc = new DOMDocument();
$doc->loadXML('<?xml version="1.0"?><foo/>');
$doc->__construct("1.1", "UTF-8");
echo $doc->saveXML();

?>
--EXPECT--
<?xml version="1.1" encoding="UTF-8"?>
32 changes: 32 additions & 0 deletions ext/dom/tests/manually_call_constructor/documentfragment.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Manually call __construct() - document fragment variation
--EXTENSIONS--
dom
--FILE--
<?php

$fragment = new DOMDocumentFragment();
var_dump($fragment->textContent);
$fragment->__construct();
var_dump($fragment->textContent);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
@$doc->documentElement->appendChild($fragment);
echo $doc->saveXML();

$fragment->__construct();
@$doc->documentElement->appendChild($fragment);
echo $doc->saveXML();

?>
--EXPECT--
string(0) ""
string(0) ""
<?xml version="1.0"?>
<container/>
<?xml version="1.0"?>
<container/>
34 changes: 34 additions & 0 deletions ext/dom/tests/manually_call_constructor/element.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Manually call __construct() - element variation
--EXTENSIONS--
dom
--FILE--
<?php

$element = new DOMElement('foo', 'my value');
var_dump($element->nodeName, $element->textContent);
$element->__construct('foo2', 'my new value');
var_dump($element->nodeName, $element->textContent);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($element);
echo $doc->saveXML();

$element->__construct('foo3', 'my new new value');
$doc->documentElement->appendChild($element);
echo $doc->saveXML();

?>
--EXPECT--
string(3) "foo"
string(8) "my value"
string(4) "foo2"
string(12) "my new value"
<?xml version="1.0"?>
<container><foo2>my new value</foo2></container>
<?xml version="1.0"?>
<container><foo2>my new value</foo2><foo3>my new new value</foo3></container>
34 changes: 34 additions & 0 deletions ext/dom/tests/manually_call_constructor/entityreference.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Manually call __construct() - entity reference variation
--EXTENSIONS--
dom
--FILE--
<?php

$entityRef = new DOMEntityReference('foo');
var_dump($entityRef->nodeName, $entityRef->textContent);
$entityRef->__construct('foo2');
var_dump($entityRef->nodeName, $entityRef->textContent);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($entityRef);
echo $doc->saveXML();

$entityRef->__construct('foo3');
$doc->documentElement->appendChild($entityRef);
echo $doc->saveXML();

?>
--EXPECT--
string(3) "foo"
string(0) ""
string(4) "foo2"
string(0) ""
<?xml version="1.0"?>
<container>&foo2;</container>
<?xml version="1.0"?>
<container>&foo2;&foo3;</container>
34 changes: 34 additions & 0 deletions ext/dom/tests/manually_call_constructor/processinginstruction.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Manually call __construct() - processing instruction variation
--EXTENSIONS--
dom
--FILE--
<?php

$pi = new DOMProcessingInstruction('name1', 'value1');
var_dump($pi->target, $pi->data);
$pi->__construct('name2', 'value2');
var_dump($pi->target, $pi->data);

$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($pi);
echo $doc->saveXML();

$pi->__construct('name3', 'value3');
$doc->documentElement->appendChild($pi);
echo $doc->saveXML();

?>
--EXPECT--
string(5) "name1"
string(6) "value1"
string(5) "name2"
string(6) "value2"
<?xml version="1.0"?>
<container><?name2 value2?></container>
<?xml version="1.0"?>
<container><?name2 value2?><?name3 value3?></container>
Loading