Skip to content

Commit 190a535

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-12167 and GH-12169: Unable to get comment or processing instruction contents in SimpleXML
2 parents 8fb51d4 + 9c37a02 commit 190a535

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ PHP NEWS
3535
. Fixed bug GH-12208 (SimpleXML infinite loop when a cast is used inside a
3636
foreach). (nielsdos)
3737
. Fixed bug #55098 (SimpleXML iteration produces infinite loop). (nielsdos)
38+
. Fixed bug GH-12167 (Unable to get processing instruction contents in
39+
SimpleXML). (nielsdos)
40+
. Fixed bug GH-12169 (Unable to get comment contents in SimpleXML).
41+
(nielsdos)
3842

3943
- Streams:
4044
. Fixed bug GH-12190 (binding ipv4 address with both address and port at 0).

ext/simplexml/simplexml.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,7 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
18281828
{
18291829
php_sxe_object *sxe;
18301830
xmlChar *contents = NULL;
1831+
bool free_contents = true;
18311832
xmlNodePtr node;
18321833
zend_result rv;
18331834

@@ -1858,13 +1859,16 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
18581859
if (sxe->node && sxe->node->node) {
18591860
if (sxe->node->node->children) {
18601861
contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1);
1862+
} else if (sxe->node->node->type == XML_COMMENT_NODE || sxe->node->node->type == XML_PI_NODE) {
1863+
contents = sxe->node->node->content;
1864+
free_contents = false;
18611865
}
18621866
}
18631867
}
18641868

18651869
rv = cast_object(writeobj, type, (char *)contents);
18661870

1867-
if (contents) {
1871+
if (contents && free_contents) {
18681872
xmlFree(contents);
18691873
}
18701874

ext/simplexml/tests/gh12167.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-12167 (Unable to get processing instruction contents in SimpleXML)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = <<<XML
9+
<?xml version="1.0"?>
10+
<container>
11+
<?foo pi contents ?>
12+
</container>
13+
XML;
14+
15+
$sxe = simplexml_load_string($xml);
16+
17+
var_dump($sxe->xpath("//processing-instruction()")[0]->getName());
18+
var_dump((string) $sxe->xpath("//processing-instruction()")[0]);
19+
20+
?>
21+
--EXPECT--
22+
string(3) "foo"
23+
string(12) "pi contents "

ext/simplexml/tests/gh12169.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-12169 (Unable to get comment contents in SimpleXML)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = <<<XML
9+
<?xml version="1.0"?>
10+
<container>
11+
<!-- comment contents -->
12+
</container>
13+
XML;
14+
15+
$sxe = simplexml_load_string($xml);
16+
17+
var_dump($sxe->xpath("//comment()")[0]->getName());
18+
var_dump((string) $sxe->xpath("//comment()")[0]);
19+
20+
?>
21+
--EXPECT--
22+
string(7) "comment"
23+
string(18) " comment contents "

0 commit comments

Comments
 (0)