1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Fix manually calling __construct() on DOM classes

Closes GH-11894.
This commit is contained in:
Niels Dossche
2023-08-06 16:49:02 +02:00
parent 5cd0208e9f
commit 08c4db7f36
20 changed files with 312 additions and 54 deletions

1
NEWS
View File

@@ -17,6 +17,7 @@ PHP NEWS
. Fixed bug GH-11791 (Wrong default value of DOMDocument::xmlStandalone).
(nielsdos)
. Fix json_encode result on DOMDocument. (nielsdos)
. Fix manually calling __construct() on DOM classes. (nielsdos)
- FFI:
. Fix leaking definitions when using FFI::cdef()->new(...). (ilutov)

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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 */

View File

@@ -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;
}
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();
}
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 */

View File

@@ -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 */

View File

@@ -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);
}

View File

@@ -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 */

View File

@@ -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);
}

View File

@@ -1,16 +0,0 @@
--TEST--
DOMDocumentFragment::__construct() called twice.
--CREDITS--
Eric Lee Stewart <ericleestewart@gmail.com>
# TestFest Atlanta 2009-05-24
--EXTENSIONS--
dom
--FILE--
<?php
$fragment = new DOMDocumentFragment();
$fragment->__construct();
var_dump($fragment);
?>
--EXPECT--
object(DOMDocumentFragment)#1 (0) {
}

View File

@@ -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"/>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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"?>

View File

@@ -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/>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -0,0 +1,33 @@
--TEST--
Manually call __construct() - text variation
--EXTENSIONS--
dom
--FILE--
<?php
$text = new DOMText('my value');
var_dump($text->textContent);
$text->__construct('my new value');
var_dump($text->textContent);
$doc = new DOMDocument();
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container/>
XML);
$doc->documentElement->appendChild($text);
echo $doc->saveXML();
$text->__construct("\nmy new new value");
$doc->documentElement->appendChild($text);
echo $doc->saveXML();
?>
--EXPECT--
string(8) "my value"
string(12) "my new value"
<?xml version="1.0"?>
<container>my new value</container>
<?xml version="1.0"?>
<container>my new value
my new new value</container>

View File

@@ -51,13 +51,11 @@ PHP_METHOD(DOMText, __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, 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, nodep, (void *)intern);
}
/* }}} end DOMText::__construct */