Skip to content

Commit 5df473d

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: 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 1cf6e12 + 39a9e56 commit 5df473d

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
@@ -5,8 +5,16 @@ PHP NEWS
55
- Filter:
66
. Fix explicit FILTER_REQUIRE_SCALAR with FILTER_CALLBACK (ilutov)
77

8+
- Hash:
9+
. Fixed bug GH-12186 (segfault copying/cloning a finalized HashContext).
10+
(MaxSem)
11+
812
- SimpleXML:
913
. Fixed bug GH-12170 (Can't use xpath with comments in SimpleXML). (nielsdos)
14+
. Fixed bug GH-12192 (SimpleXML infinite loop when getName() is called
15+
within foreach). (nielsdos)
16+
. Fixed bug GH-12223 (Entity reference produces infinite loop in
17+
var_dump/print_r). (nielsdos)
1018

1119
28 Sep 2023, PHP 8.2.11
1220

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
@@ -45,6 +45,7 @@ PHP_SXE_API zend_class_entry *sxe_get_element_class_entry(void) /* {{{ */
4545

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

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

100+
static xmlNodePtr php_sxe_get_first_node_non_destructive(php_sxe_object *sxe, xmlNodePtr node)
101+
{
102+
if (sxe && sxe->iter.type != SXE_ITER_NONE) {
103+
return php_sxe_reset_iterator_no_clear_iter_data(sxe, false);
104+
} else {
105+
return node;
106+
}
107+
}
108+
98109
static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */
99110
{
100111
if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) {
@@ -1186,6 +1197,12 @@ static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
11861197
sxe_properties_add(rv, name, namelen, &value);
11871198
}
11881199
next_iter:
1200+
if (UNEXPECTED(node->type == XML_ENTITY_DECL)) {
1201+
/* Entity decls are linked together via the next pointer.
1202+
* The only way to get to an entity decl is via an entity reference in the document.
1203+
* If we then continue iterating, we'll end up in the DTD. Even worse, if the entities reference each other we'll infinite loop. */
1204+
break;
1205+
}
11891206
if (use_iter) {
11901207
node = php_sxe_iterator_fetch(sxe, node->next, 0);
11911208
} else {
@@ -1625,7 +1642,7 @@ PHP_METHOD(SimpleXMLElement, getName)
16251642
sxe = Z_SXEOBJ_P(ZEND_THIS);
16261643

16271644
GET_NODE(sxe, node);
1628-
node = php_sxe_get_first_node(sxe, node);
1645+
node = php_sxe_get_first_node_non_destructive(sxe, node);
16291646
if (node) {
16301647
namelen = xmlStrlen(node->name);
16311648
RETURN_STRINGL((char*)node->name, namelen);
@@ -2450,15 +2467,9 @@ static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, i
24502467
}
24512468
/* }}} */
24522469

2453-
static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
2470+
static xmlNodePtr php_sxe_reset_iterator_no_clear_iter_data(php_sxe_object *sxe, int use_data)
24542471
{
24552472
xmlNodePtr node;
2456-
2457-
if (!Z_ISUNDEF(sxe->iter.data)) {
2458-
zval_ptr_dtor(&sxe->iter.data);
2459-
ZVAL_UNDEF(&sxe->iter.data);
2460-
}
2461-
24622473
GET_NODE(sxe, node)
24632474

24642475
if (node) {
@@ -2471,10 +2482,23 @@ static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {
24712482
case SXE_ITER_ATTRLIST:
24722483
node = (xmlNodePtr) node->properties;
24732484
}
2485+
if (use_data) {
2486+
ZEND_ASSERT(Z_ISUNDEF(sxe->iter.data));
2487+
}
24742488
return php_sxe_iterator_fetch(sxe, node, use_data);
24752489
}
24762490
return NULL;
24772491
}
2492+
2493+
static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
2494+
{
2495+
if (!Z_ISUNDEF(sxe->iter.data)) {
2496+
zval_ptr_dtor(&sxe->iter.data);
2497+
ZVAL_UNDEF(&sxe->iter.data);
2498+
}
2499+
2500+
return php_sxe_reset_iterator_no_clear_iter_data(sxe, use_data);
2501+
}
24782502
/* }}} */
24792503

24802504
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)