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

From: Date: Fri, 06 Nov 2026 03:07:04 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143363@lists.php.net to get a copy of this message
Author: T Pusher: alexdowad Date: 2026-11-06T11:50:26+09:00 Commit: https://github.com/php/php-src/commit/13b89e20cb041467b3b194b02f8c676a82f5dacb Raw diff: https://github.com/php/php-src/commit/13b89e20cb041467b3b194b02f8c676a82f5dacb.diff Merge branch 'PHP-8.5' into PHP-8.6 Changed paths: A ext/mbstring/tests/mb_encoding_recursive_stack_limit.phpt M NEWS M ext/mbstring/mbstring.c Diff: diff --git a/NEWS b/NEWS index a25df1bb1ca4..b6b9a44d43ff 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,10 @@ PHP NEWS - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) +- MBString: + . Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with + deeply nested arrays. (Eyüp Can Akman) + - Opcache: . Fixed bug GH-22216 (spurious opcache.memory_consumption warning on each new ZTS thread; the directive is also reported as 0 in worker threads). diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index eb05a0986770..d2e17d6c0bc4 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -2813,6 +2813,17 @@ MBSTRING_API zend_string* php_mb_convert_encoding(const char *input, size_t leng return php_mb_convert_encoding_ex(input, length, to_encoding, from_encoding); } +static zend_always_inline bool mb_check_stack_limit(void) +{ +#ifdef ZEND_CHECK_STACK_LIMIT + if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { + zend_call_stack_size_error(); + return true; + } +#endif + return false; +} + MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, const mbfl_encoding *to_encoding, const mbfl_encoding **from_encodings, size_t num_from_encodings) { HashTable *output, *chash; @@ -2824,6 +2835,10 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons return NULL; } + if (mb_check_stack_limit()) { + return NULL; + } + if (GC_IS_RECURSIVE(input)) { GC_UNPROTECT_RECURSION(input); php_error_docref(NULL, E_WARNING, "Cannot convert recursively referenced values"); @@ -2867,6 +2882,13 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons Z_ARRVAL_P(entry), to_encoding, from_encodings, num_from_encodings); if (chash) { ZVAL_ARR(&entry_tmp, chash); + } else if (UNEXPECTED(EG(exception))) { + if (key) { + zend_string_release(key); + } + GC_TRY_UNPROTECT_RECURSION(input); + zend_array_destroy(output); + return NULL; } else { ZVAL_EMPTY_ARRAY(&entry_tmp); } @@ -2972,7 +2994,9 @@ PHP_FUNCTION(mb_convert_encoding) HashTable *tmp; tmp = php_mb_convert_encoding_recursive( input_ht, to_encoding, from_encodings, num_from_encodings); - RETVAL_ARR(tmp); + if (tmp) { + RETVAL_ARR(tmp); + } } if (free_from_encodings) { @@ -3774,17 +3798,6 @@ PHP_FUNCTION(mb_convert_kana) RETVAL_STR(jp_kana_convert(str, enc, opt)); } -static zend_always_inline bool mb_check_stack_limit(void) -{ -#ifdef ZEND_CHECK_STACK_LIMIT - if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { - zend_call_stack_size_error(); - return true; - } -#endif - return false; -} - static unsigned int mb_recursive_count_strings(zval *var) { unsigned int count = 0; @@ -5721,6 +5734,10 @@ static bool php_mb_check_encoding_recursive(HashTable *vars, const mbfl_encoding zval *entry; bool valid = true; + if (mb_check_stack_limit()) { + return false; + } + if (GC_IS_RECURSIVE(vars)) { php_error_docref(NULL, E_WARNING, "Cannot not handle circular references"); return false; @@ -5743,6 +5760,10 @@ static bool php_mb_check_encoding_recursive(HashTable *vars, const mbfl_encoding break; case IS_ARRAY: if (!php_mb_check_encoding_recursive(Z_ARRVAL_P(entry), encoding)) { + if (UNEXPECTED(EG(exception))) { + GC_TRY_UNPROTECT_RECURSION(vars); + return false; + } valid = false; break; } diff --git a/ext/mbstring/tests/mb_encoding_recursive_stack_limit.phpt b/ext/mbstring/tests/mb_encoding_recursive_stack_limit.phpt new file mode 100644 index 000000000000..146dcffec74e --- /dev/null +++ b/ext/mbstring/tests/mb_encoding_recursive_stack_limit.phpt @@ -0,0 +1,52 @@ +--TEST-- +Stack overflow in mb_check_encoding() and mb_convert_encoding() with deeply nested arrays +--EXTENSIONS-- +mbstring +--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 nested arrays, so the second must not be visited after the first throws. */ +foreach ([[$deep], [$deep, $deep]] as $input) { + try { + mb_check_encoding($input, 'UTF-8'); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + var_dump($e->getPrevious()); + } + try { + mb_convert_encoding($input, 'UTF-8', 'ISO-8859-1'); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + var_dump($e->getPrevious()); + } +} + +unset($input, $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 +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 (#143363) next »