Skip to content

Commit 2e8cdd8

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

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

ext/simplexml/simplexml.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
17891789
{
17901790
php_sxe_object *sxe;
17911791
xmlChar *contents = NULL;
1792+
bool free_contents = true;
17921793
xmlNodePtr node;
17931794
zend_result rv;
17941795

@@ -1819,13 +1820,16 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
18191820
if (sxe->node && sxe->node->node) {
18201821
if (sxe->node->node->children) {
18211822
contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1);
1823+
} else if (sxe->node->node->type == XML_COMMENT_NODE || sxe->node->node->type == XML_PI_NODE) {
1824+
contents = sxe->node->node->content;
1825+
free_contents = false;
18221826
}
18231827
}
18241828
}
18251829

18261830
rv = cast_object(writeobj, type, (char *)contents);
18271831

1828-
if (contents) {
1832+
if (contents && free_contents) {
18291833
xmlFree(contents);
18301834
}
18311835

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)