Skip to content

Commit 4bb27e2

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-12223: Entity reference produces infinite loop in var_dump/print_r Fix GH-12192: SimpleXML infinite loop when getName() is called within foreach Fix GH-12186: segfault copying/cloning a finalized HashContext
2 parents d344fe0 + 5df473d commit 4bb27e2

File tree

8 files changed

+189
-10
lines changed

8 files changed

+189
-10
lines changed

NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ PHP NEWS
99
- Filter:
1010
. Fix explicit FILTER_REQUIRE_SCALAR with FILTER_CALLBACK (ilutov)
1111

12+
- Hash:
13+
. Fixed bug GH-12186 (segfault copying/cloning a finalized HashContext).
14+
(MaxSem)
15+
1216
- SimpleXML:
1317
. Fixed bug GH-12170 (Can't use xpath with comments in SimpleXML). (nielsdos)
18+
. Fixed bug GH-12192 (SimpleXML infinite loop when getName() is called
19+
within foreach). (nielsdos)
20+
. Fixed bug GH-12223 (Entity reference produces infinite loop in
21+
var_dump/print_r). (nielsdos)
1422

1523
14 Sep 2023, PHP 8.3.0RC2
1624

ext/hash/hash.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ PHP_FUNCTION(hash_init)
681681

682682
#define PHP_HASHCONTEXT_VERIFY(hash) { \
683683
if (!hash->context) { \
684-
zend_argument_type_error(1, "must be a valid Hash Context resource"); \
684+
zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); \
685685
RETURN_THROWS(); \
686686
} \
687687
}
@@ -838,11 +838,15 @@ PHP_FUNCTION(hash_final)
838838
PHP_FUNCTION(hash_copy)
839839
{
840840
zval *zhash;
841+
php_hashcontext_object *context;
841842

842843
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
843844
RETURN_THROWS();
844845
}
845846

847+
context = php_hashcontext_from_object(Z_OBJ_P(zhash));
848+
PHP_HASHCONTEXT_VERIFY(context);
849+
846850
RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
847851

848852
if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
@@ -1395,6 +1399,11 @@ static zend_object *php_hashcontext_clone(zend_object *zobj) {
13951399
zend_object *znew = php_hashcontext_create(zobj->ce);
13961400
php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
13971401

1402+
if (!oldobj->context) {
1403+
zend_throw_exception(zend_ce_value_error, "Cannot clone a finalized HashContext", 0);
1404+
return znew;
1405+
}
1406+
13981407
zend_objects_clone_members(znew, zobj);
13991408

14001409
newobj->ops = oldobj->ops;

ext/hash/tests/gh12186_1.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Hash: bug #12186 - segfault in hash_copy() on a finalized context
3+
--FILE--
4+
<?php
5+
6+
$c = hash_init('sha1');
7+
hash_final($c);
8+
9+
try {
10+
hash_copy($c);
11+
} catch (Throwable $ex) {
12+
echo $ex->getMessage() . "\n";
13+
}
14+
15+
?>
16+
--EXPECTF--
17+
hash_copy(): Argument #1 ($context) must be a valid, non-finalized HashContext

ext/hash/tests/gh12186_2.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Hash: bug #12186 - segfault when cloning a finalized context
3+
--FILE--
4+
<?php
5+
6+
$c = hash_init('sha1');
7+
hash_final($c);
8+
9+
try {
10+
clone $c;
11+
} catch (Throwable $ex) {
12+
echo $ex->getMessage() . "\n";
13+
}
14+
15+
?>
16+
--EXPECTF--
17+
Cannot clone a finalized HashContext

ext/hash/tests/reuse.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ catch (\Error $e) {
1414

1515
?>
1616
--EXPECT--
17-
hash_update(): Argument #1 ($context) must be a valid Hash Context resource
17+
hash_update(): Argument #1 ($context) must be a valid, non-finalized HashContext

ext/simplexml/simplexml.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PHP_SXE_API zend_class_entry *sxe_get_element_class_entry(void) /* {{{ */
4444

4545
static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_function *fptr_count);
4646
static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data);
47+
static xmlNodePtr php_sxe_reset_iterator_no_clear_iter_data(php_sxe_object *sxe, int use_data);
4748
static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data);
4849
static void php_sxe_iterator_dtor(zend_object_iterator *iter);
4950
static int php_sxe_iterator_valid(zend_object_iterator *iter);
@@ -76,6 +77,7 @@ static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE
7677
}
7778
/* }}} */
7879

80+
/* Important: this overwrites the iterator data, if you wish to keep it use php_sxe_get_first_node_non_destructive() instead! */
7981
static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node) /* {{{ */
8082
{
8183
php_sxe_object *intern;
@@ -94,6 +96,15 @@ static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node) /
9496
}
9597
/* }}} */
9698

99+
static xmlNodePtr php_sxe_get_first_node_non_destructive(php_sxe_object *sxe, xmlNodePtr node)
100+
{
101+
if (sxe && sxe->iter.type != SXE_ITER_NONE) {
102+
return php_sxe_reset_iterator_no_clear_iter_data(sxe, false);
103+
} else {
104+
return node;
105+
}
106+
}
107+
97108
static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */
98109
{
99110
if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) {
@@ -1177,6 +1188,12 @@ static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
11771188
sxe_properties_add(rv, name, namelen, &value);
11781189
}
11791190
next_iter:
1191+
if (UNEXPECTED(node->type == XML_ENTITY_DECL)) {
1192+
/* Entity decls are linked together via the next pointer.
1193+
* The only way to get to an entity decl is via an entity reference in the document.
1194+
* If we then continue iterating, we'll end up in the DTD. Even worse, if the entities reference each other we'll infinite loop. */
1195+
break;
1196+
}
11801197
if (use_iter) {
11811198
node = php_sxe_iterator_fetch(sxe, node->next, 0);
11821199
} else {
@@ -1616,7 +1633,7 @@ PHP_METHOD(SimpleXMLElement, getName)
16161633
sxe = Z_SXEOBJ_P(ZEND_THIS);
16171634

16181635
GET_NODE(sxe, node);
1619-
node = php_sxe_get_first_node(sxe, node);
1636+
node = php_sxe_get_first_node_non_destructive(sxe, node);
16201637
if (node) {
16211638
namelen = xmlStrlen(node->name);
16221639
RETURN_STRINGL((char*)node->name, namelen);
@@ -2443,15 +2460,9 @@ static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, i
24432460
}
24442461
/* }}} */
24452462

2446-
static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
2463+
static xmlNodePtr php_sxe_reset_iterator_no_clear_iter_data(php_sxe_object *sxe, int use_data)
24472464
{
24482465
xmlNodePtr node;
2449-
2450-
if (!Z_ISUNDEF(sxe->iter.data)) {
2451-
zval_ptr_dtor(&sxe->iter.data);
2452-
ZVAL_UNDEF(&sxe->iter.data);
2453-
}
2454-
24552466
GET_NODE(sxe, node)
24562467

24572468
if (node) {
@@ -2464,10 +2475,23 @@ static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {
24642475
case SXE_ITER_ATTRLIST:
24652476
node = (xmlNodePtr) node->properties;
24662477
}
2478+
if (use_data) {
2479+
ZEND_ASSERT(Z_ISUNDEF(sxe->iter.data));
2480+
}
24672481
return php_sxe_iterator_fetch(sxe, node, use_data);
24682482
}
24692483
return NULL;
24702484
}
2485+
2486+
static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
2487+
{
2488+
if (!Z_ISUNDEF(sxe->iter.data)) {
2489+
zval_ptr_dtor(&sxe->iter.data);
2490+
ZVAL_UNDEF(&sxe->iter.data);
2491+
}
2492+
2493+
return php_sxe_reset_iterator_no_clear_iter_data(sxe, use_data);
2494+
}
24712495
/* }}} */
24722496

24732497
zend_object_iterator *php_sxe_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */

ext/simplexml/tests/gh12192.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-12192 (SimpleXML infinite loop when getName() is called within foreach)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = "<root><a>1</a><a>2</a></root>";
9+
$xml = simplexml_load_string($xml);
10+
11+
$a = $xml->a;
12+
13+
foreach ($a as $test) {
14+
echo "Iteration\n";
15+
var_dump($a->key());
16+
var_dump($a->getName());
17+
var_dump((string) $test);
18+
}
19+
20+
var_dump($a);
21+
22+
?>
23+
--EXPECT--
24+
Iteration
25+
string(1) "a"
26+
string(1) "a"
27+
string(1) "1"
28+
Iteration
29+
string(1) "a"
30+
string(1) "a"
31+
string(1) "2"
32+
object(SimpleXMLElement)#2 (2) {
33+
[0]=>
34+
string(1) "1"
35+
[1]=>
36+
string(1) "2"
37+
}

ext/simplexml/tests/gh12223.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
GH-12223: Entity reference produces infinite loop in var_dump/print_r
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = <<<XML
9+
<?xml version="1.0"?>
10+
<!DOCTYPE somedoc [
11+
<!ENTITY a "something">
12+
<!ENTITY b "&a;">
13+
<!ENTITY c "&b;">
14+
]>
15+
<somedoc>&c;</somedoc>
16+
XML;
17+
18+
$sxe = simplexml_load_string($xml);
19+
20+
var_dump($sxe);
21+
print_r($sxe);
22+
23+
?>
24+
--EXPECT--
25+
object(SimpleXMLElement)#1 (1) {
26+
["c"]=>
27+
object(SimpleXMLElement)#2 (1) {
28+
["c"]=>
29+
object(SimpleXMLElement)#3 (1) {
30+
["b"]=>
31+
object(SimpleXMLElement)#4 (1) {
32+
["b"]=>
33+
object(SimpleXMLElement)#5 (1) {
34+
["a"]=>
35+
object(SimpleXMLElement)#6 (1) {
36+
["a"]=>
37+
string(9) "something"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
SimpleXMLElement Object
45+
(
46+
[c] => SimpleXMLElement Object
47+
(
48+
[c] => SimpleXMLElement Object
49+
(
50+
[b] => SimpleXMLElement Object
51+
(
52+
[b] => SimpleXMLElement Object
53+
(
54+
[a] => SimpleXMLElement Object
55+
(
56+
[a] => something
57+
)
58+
59+
)
60+
61+
)
62+
63+
)
64+
65+
)
66+
67+
)

0 commit comments

Comments
 (0)