diff --git a/NEWS b/NEWS index 474db936ef15..085119ff9d44 100644 --- a/NEWS +++ b/NEWS @@ -48,6 +48,10 @@ PHP NEWS - SQLite: . Fix leak when trying to close db if blob stream is still open. (ndossche) +- Standard: + . Fixed bug GH-23089 (Stack overflow in array_merge_recursive() with deeply + nested arrays). (Lazizbek Ergashev) + - Streams: . Fixed bug GH-15836 (Use-after-free when a user stream filter accesses $this->stream during the close flush). (iliaal) diff --git a/ext/standard/array.c b/ext/standard/array.c index 4527d9a80df8..49c90b88bca4 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4047,6 +4047,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) { diff --git a/ext/standard/tests/array/gh23089.phpt b/ext/standard/tests/array/gh23089.phpt new file mode 100644 index 000000000000..e59900ec85d8 --- /dev/null +++ b/ext/standard/tests/array/gh23089.phpt @@ -0,0 +1,27 @@ +--TEST-- +GH-23089 (Stack overflow in array_merge_recursive with deeply nested arrays) +--SKIPIF-- + +--INI-- +zend.max_allowed_stack_size=256K +--FILE-- + $a]; +} +try { + array_merge_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?