Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ PHP NEWS
(ndossche)

- Standard:
. Fixed bug GH-23089 (Stack overflow in array_merge_recursive() with deeply
nested arrays). (Lazizbek Ergashev, akasakariko)
. Fixed dns_get_record() returning partially built records for malformed
DNS responses. (Marti2203)

Expand Down
7 changes: 7 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,13 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
zval *src_entry, *dest_entry;
zend_string *string_key;

#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_call_stack_size_error();
return 0;
}
#endif

ZEND_HASH_FOREACH_STR_KEY_VAL(src, string_key, src_entry) {
if (string_key) {
if ((dest_entry = zend_hash_find_known_hash(dest, string_key)) != NULL) {
Expand Down
30 changes: 30 additions & 0 deletions ext/standard/tests/array/gh23089.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-23089 (Stack overflow in array_merge_recursive with deeply nested arrays)
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
--FILE--
<?php
$a = [];
for ($i = 0; $i < 30000; $i++) {
$a = ['k' => $a];
}
try {
array_merge_recursive($a, $a);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

/* Avoid the separate stack overflow during array destruction (GH-15869). */
unset($e);
while ($a) {
$a = $a['k'];
}
?>
--EXPECTF--
Error: Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
Loading