Skip to content

Commit c160693

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix phpGH-11433: Unable to set CURLOPT_ACCEPT_ENCODING to NULL Fix "invalid state error" with cloned namespace declarations Fix lifetime issue with getAttributeNodeNS()
2 parents 2cbb0c0 + a8a3b99 commit c160693

7 files changed

+185
-30
lines changed

NEWS

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ PHP NEWS
99
- Core:
1010
. Fixed build for the riscv64 architecture/GCC 12. (Daniil Gentili)
1111

12+
- Curl:
13+
. Fixed bug GH-11433 (Unable to set CURLOPT_ACCEPT_ENCODING to NULL).
14+
(nielsdos)
15+
1216
- DOM:
1317
. Fixed bugs GH-11288 and GH-11289 and GH-11290 and GH-9142 (DOMExceptions
1418
and segfaults with replaceWith). (nielsdos)
@@ -30,6 +34,8 @@ PHP NEWS
3034
. Fixed bug #70359 (print_r() on DOMAttr causes Segfault in
3135
php_libxml_node_free_list()). (nielsdos)
3236
. Fixed bug #78577 (Crash in DOMNameSpace debug info handlers). (nielsdos)
37+
. Fix lifetime issue with getAttributeNodeNS(). (nielsdos)
38+
. Fix "invalid state error" with cloned namespace declarations. (nielsdos)
3339

3440
- Opcache:
3541
. Fix allocation loop in zend_shared_alloc_startup(). (nielsdos)

ext/curl/interface.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,6 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
18351835
case CURLOPT_TLSAUTH_TYPE:
18361836
case CURLOPT_TLSAUTH_PASSWORD:
18371837
case CURLOPT_TLSAUTH_USERNAME:
1838-
case CURLOPT_ACCEPT_ENCODING:
18391838
case CURLOPT_TRANSFER_ENCODING:
18401839
case CURLOPT_DNS_SERVERS:
18411840
case CURLOPT_MAIL_AUTH:
@@ -1910,6 +1909,7 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
19101909
case CURLOPT_RANGE:
19111910
case CURLOPT_FTP_ACCOUNT:
19121911
case CURLOPT_RTSP_SESSION_ID:
1912+
case CURLOPT_ACCEPT_ENCODING:
19131913
#if LIBCURL_VERSION_NUM >= 0x072100 /* Available since 7.33.0 */
19141914
case CURLOPT_DNS_INTERFACE:
19151915
case CURLOPT_DNS_LOCAL_IP4:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Test curl_setopt() with CURLOPT_ACCEPT_ENCODING
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
8+
include 'server.inc';
9+
$host = curl_cli_server_start();
10+
11+
$ch = curl_init();
12+
13+
$url = "{$host}/get.inc?test=";
14+
curl_setopt($ch, CURLOPT_URL, $url);
15+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
16+
curl_setopt($ch, CURLOPT_ACCEPT_ENCODING, "gzip");
17+
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
18+
19+
// First execution, with gzip accept
20+
curl_exec($ch);
21+
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
22+
23+
// Second execution, with the encoding accept disabled
24+
curl_setopt($ch, CURLOPT_ACCEPT_ENCODING, NULL);
25+
curl_exec($ch);
26+
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
27+
28+
curl_close($ch);
29+
?>
30+
--EXPECTF--
31+
GET /get.inc?test= HTTP/1.1
32+
Host: %s
33+
Accept: */*
34+
Accept-Encoding: gzip
35+
36+
GET /get.inc?test= HTTP/1.1
37+
Host: %s
38+
Accept: */*

ext/dom/element.c

+4-16
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ Since: DOM Level 2
787787
PHP_METHOD(DOMElement, getAttributeNodeNS)
788788
{
789789
zval *id;
790-
xmlNodePtr elemp, fakeAttrp;
790+
xmlNodePtr elemp;
791791
xmlAttrPtr attrp;
792792
dom_object *intern;
793793
size_t uri_len, name_len;
@@ -808,21 +808,9 @@ PHP_METHOD(DOMElement, getAttributeNodeNS)
808808
xmlNsPtr nsptr;
809809
nsptr = dom_get_nsdecl(elemp, (xmlChar *)name);
810810
if (nsptr != NULL) {
811-
xmlNsPtr curns;
812-
curns = xmlNewNs(NULL, nsptr->href, NULL);
813-
if (nsptr->prefix) {
814-
curns->prefix = xmlStrdup((xmlChar *) nsptr->prefix);
815-
}
816-
if (nsptr->prefix) {
817-
fakeAttrp = xmlNewDocNode(elemp->doc, NULL, (xmlChar *) nsptr->prefix, nsptr->href);
818-
} else {
819-
fakeAttrp = xmlNewDocNode(elemp->doc, NULL, (xmlChar *)"xmlns", nsptr->href);
820-
}
821-
fakeAttrp->type = XML_NAMESPACE_DECL;
822-
fakeAttrp->parent = elemp;
823-
fakeAttrp->ns = curns;
824-
825-
DOM_RET_OBJ(fakeAttrp, &ret, intern);
811+
/* Keep parent alive, because we're a fake child. */
812+
GC_ADDREF(&intern->std);
813+
(void) php_dom_create_fake_namespace_decl(elemp, nsptr, return_value, intern);
826814
} else {
827815
RETURN_NULL();
828816
}

ext/dom/php_dom.c

+44-13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static HashTable dom_xpath_prop_handlers;
8989

9090
static zend_object *dom_objects_namespace_node_new(zend_class_entry *class_type);
9191
static void dom_object_namespace_node_free_storage(zend_object *object);
92+
static xmlNodePtr php_dom_create_fake_namespace_decl_node_ptr(xmlNodePtr nodep, xmlNsPtr original);
9293

9394
typedef int (*dom_read_t)(dom_object *obj, zval *retval);
9495
typedef int (*dom_write_t)(dom_object *obj, zval *newval);
@@ -477,6 +478,19 @@ PHP_FUNCTION(dom_import_simplexml)
477478

478479
static dom_object* dom_objects_set_class(zend_class_entry *class_type);
479480

481+
static void dom_update_refcount_after_clone(dom_object *original, xmlNodePtr original_node, dom_object *clone, xmlNodePtr cloned_node)
482+
{
483+
/* If we cloned a document then we must create new doc proxy */
484+
if (cloned_node->doc == original_node->doc) {
485+
clone->document = original->document;
486+
}
487+
php_libxml_increment_doc_ref((php_libxml_node_object *)clone, cloned_node->doc);
488+
php_libxml_increment_node_ptr((php_libxml_node_object *)clone, cloned_node, (void *)clone);
489+
if (original->document != clone->document) {
490+
dom_copy_doc_props(original->document, clone->document);
491+
}
492+
}
493+
480494
static zend_object *dom_objects_store_clone_obj(zend_object *zobject) /* {{{ */
481495
{
482496
dom_object *intern = php_dom_obj_from_obj(zobject);
@@ -489,15 +503,7 @@ static zend_object *dom_objects_store_clone_obj(zend_object *zobject) /* {{{ */
489503
if (node != NULL) {
490504
xmlNodePtr cloned_node = xmlDocCopyNode(node, node->doc, 1);
491505
if (cloned_node != NULL) {
492-
/* If we cloned a document then we must create new doc proxy */
493-
if (cloned_node->doc == node->doc) {
494-
clone->document = intern->document;
495-
}
496-
php_libxml_increment_doc_ref((php_libxml_node_object *)clone, cloned_node->doc);
497-
php_libxml_increment_node_ptr((php_libxml_node_object *)clone, cloned_node, (void *)clone);
498-
if (intern->document != clone->document) {
499-
dom_copy_doc_props(intern->document, clone->document);
500-
}
506+
dom_update_refcount_after_clone(intern, node, clone, cloned_node);
501507
}
502508

503509
}
@@ -509,6 +515,26 @@ static zend_object *dom_objects_store_clone_obj(zend_object *zobject) /* {{{ */
509515
}
510516
/* }}} */
511517

518+
static zend_object *dom_object_namespace_node_clone_obj(zend_object *zobject)
519+
{
520+
dom_object_namespace_node *intern = php_dom_namespace_node_obj_from_obj(zobject);
521+
zend_object *clone = dom_objects_namespace_node_new(intern->dom.std.ce);
522+
dom_object_namespace_node *clone_intern = php_dom_namespace_node_obj_from_obj(clone);
523+
524+
xmlNodePtr original_node = dom_object_get_node(&intern->dom);
525+
ZEND_ASSERT(original_node->type == XML_NAMESPACE_DECL);
526+
xmlNodePtr cloned_node = php_dom_create_fake_namespace_decl_node_ptr(original_node->parent, original_node->ns);
527+
528+
if (intern->parent_intern) {
529+
clone_intern->parent_intern = intern->parent_intern;
530+
GC_ADDREF(&clone_intern->parent_intern->std);
531+
}
532+
dom_update_refcount_after_clone(&intern->dom, original_node, &clone_intern->dom, cloned_node);
533+
534+
zend_objects_clone_members(clone, &intern->dom.std);
535+
return clone;
536+
}
537+
512538
static void dom_copy_prop_handler(zval *zv) /* {{{ */
513539
{
514540
dom_prop_handler *hnd = Z_PTR_P(zv);
@@ -577,6 +603,7 @@ PHP_MINIT_FUNCTION(dom)
577603
memcpy(&dom_object_namespace_node_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
578604
dom_object_namespace_node_handlers.offset = XtOffsetOf(dom_object_namespace_node, dom.std);
579605
dom_object_namespace_node_handlers.free_obj = dom_object_namespace_node_free_storage;
606+
dom_object_namespace_node_handlers.clone_obj = dom_object_namespace_node_clone_obj;
580607

581608
zend_hash_init(&classes, 0, NULL, NULL, 1);
582609

@@ -1530,8 +1557,7 @@ xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) {
15301557
}
15311558
/* }}} end dom_get_nsdecl */
15321559

1533-
/* Note: Assumes the additional lifetime was already added in the caller. */
1534-
xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern)
1560+
static xmlNodePtr php_dom_create_fake_namespace_decl_node_ptr(xmlNodePtr nodep, xmlNsPtr original)
15351561
{
15361562
xmlNodePtr attrp;
15371563
xmlNsPtr curns = xmlNewNs(NULL, original->href, NULL);
@@ -1544,11 +1570,16 @@ xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr origina
15441570
attrp->type = XML_NAMESPACE_DECL;
15451571
attrp->parent = nodep;
15461572
attrp->ns = curns;
1573+
return attrp;
1574+
}
15471575

1576+
/* Note: Assumes the additional lifetime was already added in the caller. */
1577+
xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern)
1578+
{
1579+
xmlNodePtr attrp = php_dom_create_fake_namespace_decl_node_ptr(nodep, original);
15481580
php_dom_create_object(attrp, return_value, parent_intern);
15491581
/* This object must exist, because we just created an object for it via php_dom_create_object(). */
1550-
dom_object *obj = ((php_libxml_node_ptr *)attrp->_private)->_private;
1551-
php_dom_namespace_node_obj_from_obj(&obj->std)->parent_intern = parent_intern;
1582+
php_dom_namespace_node_obj_from_obj(Z_OBJ_P(return_value))->parent_intern = parent_intern;
15521583
return attrp;
15531584
}
15541585

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Lifetime issue with parentNode on getAttributeNodeNS()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$xmlString = '<?xml version="1.0" encoding="utf-8" ?>
8+
<root xmlns="http://ns" xmlns:ns2="http://ns2">
9+
<ns2:child />
10+
</root>';
11+
12+
$xml=new DOMDocument();
13+
$xml->loadXML($xmlString);
14+
$ns2 = $xml->documentElement->getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "ns2");
15+
$ns2->parentNode->remove();
16+
var_dump($ns2->parentNode->localName);
17+
18+
?>
19+
--EXPECT--
20+
string(4) "root"

ext/dom/tests/clone_nodes.phpt

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--TEST--
2+
Clone nodes
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
echo "-- Clone DOMNameSpaceNode --\n";
9+
10+
$doc = new DOMDocument;
11+
$doc->loadXML('<foo xmlns="http://php.net/test" xmlns:foo="urn:foo" />');
12+
13+
$attr = $doc->documentElement->getAttributeNode('xmlns');
14+
var_dump($attr);
15+
16+
$attrClone = clone $attr;
17+
var_dump($attrClone->nodeValue);
18+
var_dump($attrClone->parentNode->nodeName);
19+
20+
unset($doc);
21+
unset($attr);
22+
23+
var_dump($attrClone->nodeValue);
24+
var_dump($attrClone->parentNode->nodeName);
25+
26+
echo "-- Clone DOMNode --\n";
27+
28+
$doc = new DOMDocument;
29+
$doc->loadXML('<foo><bar/></foo>');
30+
31+
$bar = $doc->documentElement->firstChild;
32+
$barClone = clone $bar;
33+
$bar->remove();
34+
unset($bar);
35+
36+
var_dump($barClone->nodeName);
37+
38+
$doc->firstElementChild->remove();
39+
unset($doc);
40+
41+
var_dump($barClone->nodeName);
42+
var_dump($barClone->parentNode);
43+
44+
?>
45+
--EXPECT--
46+
-- Clone DOMNameSpaceNode --
47+
object(DOMNameSpaceNode)#3 (8) {
48+
["nodeName"]=>
49+
string(5) "xmlns"
50+
["nodeValue"]=>
51+
string(19) "http://php.net/test"
52+
["nodeType"]=>
53+
int(18)
54+
["prefix"]=>
55+
string(0) ""
56+
["localName"]=>
57+
string(5) "xmlns"
58+
["namespaceURI"]=>
59+
string(19) "http://php.net/test"
60+
["ownerDocument"]=>
61+
string(22) "(object value omitted)"
62+
["parentNode"]=>
63+
string(22) "(object value omitted)"
64+
}
65+
string(19) "http://php.net/test"
66+
string(3) "foo"
67+
string(19) "http://php.net/test"
68+
string(3) "foo"
69+
-- Clone DOMNode --
70+
string(3) "bar"
71+
string(3) "bar"
72+
NULL

0 commit comments

Comments
 (0)