[php-src] master: Merge branch 'PHP-8.6'
| From: | T | Date: | Fri, 06 Nov 2026 03:07:39 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143364@lists.php.net to get a copy of this message | ||
Author: T
Pusher: alexdowad
Date: 2026-11-06T12:05:35+09:00
Commit: https://github.com/php/php-src/commit/33cf512823e758e1274ef7caab2543e285811821
Raw diff: https://github.com/php/php-src/commit/33cf512823e758e1274ef7caab2543e285811821.diff
Merge branch '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 e25c7e87a939..23af26473d04 100644
--- a/NEWS
+++ b/NEWS
@@ -63,6 +63,8 @@ PHP NEWS
. Fixed mbstring functions emitting surrogates in UTF-8 output and flagging
it as valid UTF-8. (Nicolas Grekas)
. Fixed bug GH-23958 (truncated encoding names resolving to full names). (Kamil Tekiela)
+ . Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with
+ deeply nested arrays. (Eyüp Can Akman)
- MySQLi:
. Fix GH-22854: Fixed failed assertion when accessing mysqli property after
diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c
index 2182ae53ed24..16e969a3ea32 100644
--- a/ext/mbstring/mbstring.c
+++ b/ext/mbstring/mbstring.c
@@ -2851,6 +2851,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;
@@ -2862,6 +2873,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");
@@ -2905,6 +2920,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);
}
@@ -3010,7 +3032,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) {
@@ -3811,17 +3835,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;
@@ -5758,6 +5771,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;
@@ -5780,6 +5797,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