[php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Thu, 12 Nov 2026 08:53:24 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143621@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Date: 2026-11-12T16:15:27+08:00 Commit: https://github.com/php/php-src/commit/0593cf066e7121bfe01a1a10b60008bd619c79bd Raw diff: https://github.com/php/php-src/commit/0593cf066e7121bfe01a1a10b60008bd619c79bd.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: Fix stack overflow in filter_var() and filter_var_array() (#25349) Changed paths: A ext/filter/tests/filter_recursive_stack_limit.phpt M NEWS M ext/filter/filter.c Diff: diff --git a/NEWS b/NEWS index f35834e6d777..199282884a7b 100644 --- a/NEWS +++ b/NEWS @@ -37,6 +37,10 @@ PHP NEWS . Fixed bug GH-25306 (Assertion status == LXB_STATUS_OK in dom_parse_selector when lexbor allocation fails). (ndossche) +- Filter: + . Fixed stack overflow in filter_var() and filter_var_array() with deeply + nested arrays. (Eyüp Can Akman) + - MBString: . Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with deeply nested arrays. (Eyüp Can Akman) diff --git a/ext/filter/filter.c b/ext/filter/filter.c index c0682037c8c2..cd021390f264 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -424,6 +424,13 @@ static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long f if (Z_TYPE_P(value) == IS_ARRAY) { zval *element; +#ifdef ZEND_CHECK_STACK_LIMIT + if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { + zend_call_stack_size_error(); + return; + } +#endif + if (Z_IS_RECURSIVE_P(value)) { return; } @@ -434,6 +441,10 @@ static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long f if (Z_TYPE_P(element) == IS_ARRAY) { SEPARATE_ARRAY(element); php_zval_filter_recursive(element, filter, flags, options, charset); + if (EG(exception)) { + Z_UNPROTECT_RECURSION_P(value); + return; + } } else { php_zval_filter(element, filter, flags, options, charset); } @@ -665,6 +676,7 @@ static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op 2 ); if (EG(exception)) { + zval_ptr_dtor(&nval); RETURN_THROWS(); } zend_hash_update(Z_ARRVAL_P(return_value), arg_key, &nval); diff --git a/ext/filter/tests/filter_recursive_stack_limit.phpt b/ext/filter/tests/filter_recursive_stack_limit.phpt new file mode 100644 index 000000000000..fb8d4c6f3854 --- /dev/null +++ b/ext/filter/tests/filter_recursive_stack_limit.phpt @@ -0,0 +1,47 @@ +--TEST-- +Stack overflow in filter_var() and filter_var_array() with deeply nested arrays +--EXTENSIONS-- +filter +--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 +$deep = 'x'; +for ($i = 0; $i < 30000; $i++) { + $deep = [$deep]; +} + +/* Two top-level entries: the second must not be visited once the + * stack limit error has been thrown, so only one Error is thrown. */ +try { + filter_var([$deep, $deep], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); +} catch (Error $e) { + echo $e->getMessage(), "\n"; + var_dump($e->getPrevious()); +} +try { + filter_var_array(['a' => $deep, 'b' => $deep], ['a' => ['flags' => FILTER_REQUIRE_ARRAY], 'b' => ['flags' => FILTER_REQUIRE_ARRAY]]); +} catch (Error $e) { + echo $e->getMessage(), "\n"; + var_dump($e->getPrevious()); +} + +unset($e); +while (is_array($deep)) { + $deep = $deep[0]; +} +?> +--EXPECTF-- +Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion? +NULL +Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion? +NULL

« previous php.cvs (#143621) next »