Skip to content
Merged
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 @@ -72,6 +72,8 @@ PHP NEWS
- Standard:
. Fixed bug GH-23111 (Stack overflow in array_walk_recursive() with deeply
nested arrays). (Lazizbek Ergashev)
. Fixed bug GH-23113 (Stack overflow in array_replace_recursive() with deeply
nested arrays). (Lazizbek Ergashev)

- Streams:
. Fixed bug GH-15836 (Use-after-free when a user stream filter accesses
Expand Down
7 changes: 7 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4168,6 +4168,13 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ *
zend_ulong num_key;
int ret;

#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_KEY_VAL(src, num_key, string_key, src_entry) {
src_zval = src_entry;
ZVAL_DEREF(src_zval);
Expand Down
27 changes: 27 additions & 0 deletions ext/standard/tests/array/gh23113.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-23113 (Stack overflow in array_replace_recursive with deeply nested arrays)
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting due to more stack space usage');
}
?>
--INI--
zend.max_allowed_stack_size=256K
--FILE--
<?php
$a = [];
for ($i = 0; $i < 30000; $i++) {
$a = ['k' => $a];
}
try {
array_replace_recursive($a, $a);
} catch (Throwable $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
}
?>
--EXPECTF--
Error: Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
Loading