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

From: Date: Thu, 12 Nov 2026 08:53:24 +0000
Subject: [php-src] master: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143620@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Date: 2026-11-12T16:17:38+08:00 Commit: https://github.com/php/php-src/commit/13bdad88c84cfb077e8670c8065999cf9b9a9e83 Raw diff: https://github.com/php/php-src/commit/13bdad88c84cfb077e8670c8065999cf9b9a9e83.diff Merge branch 'PHP-8.5' into PHP-8.6 * PHP-8.5: 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 a90115aef2dd..567e9baf8218 100644 --- a/NEWS +++ b/NEWS @@ -53,6 +53,10 @@ PHP NEWS since libmagic 5.48). (Weilin Du) . Fixed incorrect MIME type detection for HWPX files. (Weilin Du) +- 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 c73f7abd612e..adabe6e6ee9a 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -421,6 +421,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; } @@ -431,6 +438,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); } @@ -658,6 +669,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 (#143620) next »