Skip to content

Commit 82a84d0

Browse files
committedSep 25, 2023
Fix GH-12167 and GH-12169: Unable to get comment or processing instruction contents in SimpleXML
Closes GH-12289.
1 parent d65c800 commit 82a84d0

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed
 

‎NEWS

+4
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,7 @@ static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type)
18351835
{
18361836
php_sxe_object *sxe;
18371837
xmlChar *contents = NULL;
1838+
bool free_contents = true;
18381839
xmlNodePtr node;
18391840
int rv;
18401841

@@ -1865,13 +1866,16 @@ static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type)
18651866
if (sxe->node && sxe->node->node) {
18661867
if (sxe->node->node->children) {
18671868
contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1);
1869+
} else if (sxe->node->node->type == XML_COMMENT_NODE || sxe->node->node->type == XML_PI_NODE) {
1870+
contents = sxe->node->node->content;
1871+
free_contents = false;
18681872
}
18691873
}
18701874
}
18711875

18721876
rv = cast_object(writeobj, type, (char *)contents);
18731877

1874-
if (contents) {
1878+
if (contents && free_contents) {
18751879
xmlFree(contents);
18761880
}
18771881

‎ext/simplexml/tests/gh12167.phpt

+23
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

+23
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)
Please sign in to comment.