Commit af1abdfc34b for php.net
commit af1abdfc34b0f4fb0ad9cfef5533a989bc4a02ab
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date: Mon Aug 10 14:59:11 2026 +0500
Add a stack limit check in php_array_replace_recursive (#23124)
php_array_replace_recursive() recurses once per nesting level with no stack check, so array_replace_recursive() on a deeply nested array exhausts the native stack and the process dies with a segfault.
Fixes GH-23113
diff --git a/NEWS b/NEWS
index 01eb4160a20..d7dadb551bc 100644
--- a/NEWS
+++ b/NEWS
@@ -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
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 6863586c81f..41123d43bcc 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -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);
diff --git a/ext/standard/tests/array/gh23113.phpt b/ext/standard/tests/array/gh23113.phpt
new file mode 100644
index 00000000000..894e8d971e5
--- /dev/null
+++ b/ext/standard/tests/array/gh23113.phpt
@@ -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?