Commit 8e36329c161 for php
commit 8e36329c1616e4f188f9cc2eefb605c82c8654c8
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Thu Nov 19 08:31:57 2026 -0500
ext/mbstring: Fix refcount info for mb_encode_mimeheader(), mb_output_handler()
mb_encode_mimeheader() returns its input as is when, after any leading
spaces, it holds only printable ASCII with no spaces and no '=', '?' or '_',
and mb_output_handler() does the same when no output conversion applies, so
the returned string can have a refcount above 1. Both were annotated
@refcount 1, which lets the JIT destroy a discarded result without checking
its refcount and free a string that is still referenced.
Closes GH-25599
diff --git a/NEWS b/NEWS
index 2415ca123f7..2f4e243efe7 100644
--- a/NEWS
+++ b/NEWS
@@ -73,6 +73,8 @@ PHP NEWS
- MBString:
. Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with
deeply nested arrays. (Eyüp Can Akman)
+ . Fixed the JIT freeing the string returned by mb_encode_mimeheader() or
+ mb_output_handler() while it is still in use. (Ilia Alshanetsky)
- Opcache:
. Fixed bug GH-24801 (JIT: SIGFPE in ir_fold MUL_OV constant folding).
diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index 7006bd6c815..6a484490b34 100644
--- a/Zend/Optimizer/zend_func_infos.h
+++ b/Zend/Optimizer/zend_func_infos.h
@@ -206,7 +206,6 @@ static const func_info_t func_infos[] = {
F1("mb_detect_order", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_TRUE),
F1("mb_substitute_character", MAY_BE_STRING|MAY_BE_LONG|MAY_BE_BOOL),
F1("mb_preferred_mime_name", MAY_BE_STRING|MAY_BE_FALSE),
- F1("mb_output_handler", MAY_BE_STRING),
F1("mb_strstr", MAY_BE_STRING|MAY_BE_FALSE),
F1("mb_strrchr", MAY_BE_STRING|MAY_BE_FALSE),
F1("mb_stristr", MAY_BE_STRING|MAY_BE_FALSE),
@@ -220,7 +219,6 @@ static const func_info_t func_infos[] = {
F1("mb_detect_encoding", MAY_BE_STRING|MAY_BE_FALSE),
FN("mb_list_encodings", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("mb_encoding_aliases", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
- F1("mb_encode_mimeheader", MAY_BE_STRING),
F1("mb_decode_mimeheader", MAY_BE_STRING),
F1("mb_convert_kana", MAY_BE_STRING),
F1("mb_convert_variables", MAY_BE_STRING|MAY_BE_FALSE),
diff --git a/ext/mbstring/mbstring.stub.php b/ext/mbstring/mbstring.stub.php
index af9c5cbb93e..02e3c36d029 100644
--- a/ext/mbstring/mbstring.stub.php
+++ b/ext/mbstring/mbstring.stub.php
@@ -81,7 +81,6 @@ function mb_preferred_mime_name(string $encoding): string|false {}
/** @param array $result */
function mb_parse_str(string $string, &$result): bool {}
-/** @refcount 1 */
function mb_output_handler(string $string, int $status): string {}
function mb_str_split(string $string, int $length = 1, ?string $encoding = null): array {}
@@ -159,7 +158,6 @@ function mb_list_encodings(): array {}
*/
function mb_encoding_aliases(string $encoding): array {}
-/** @refcount 1 */
function mb_encode_mimeheader(string $string, ?string $charset = null, ?string $transfer_encoding = null, string $newline = "\r\n", int $indent = 0): string {}
/** @refcount 1 */
diff --git a/ext/mbstring/mbstring_arginfo.h b/ext/mbstring/mbstring_arginfo.h
index 230dddf9694..3fa13bd7bd9 100644
Binary files a/ext/mbstring/mbstring_arginfo.h and b/ext/mbstring/mbstring_arginfo.h differ
diff --git a/ext/mbstring/tests/jit_returned_input_refcount.phpt b/ext/mbstring/tests/jit_returned_input_refcount.phpt
new file mode 100644
index 00000000000..a01954f7bfe
--- /dev/null
+++ b/ext/mbstring/tests/jit_returned_input_refcount.phpt
@@ -0,0 +1,30 @@
+--TEST--
+mb_encode_mimeheader() and mb_output_handler() returning their input string under function JIT
+--EXTENSIONS--
+mbstring
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.jit=function
+opcache.jit_buffer_size=64M
+opcache.file_update_protection=0
+--FILE--
+<?php
+$s = str_repeat("a", mt_rand(3, 3));
+mb_encode_mimeheader($s);
+mb_http_output("UTF-8");
+mb_output_handler($s, 0);
+mb_http_output("pass");
+mb_output_handler($s, 0);
+echo "input after calls: ";
+var_dump($s);
+echo "mb_encode_mimeheader() returns input: ";
+var_dump(mb_encode_mimeheader($s) === $s);
+echo "mb_output_handler() returns input: ";
+var_dump(mb_output_handler($s, 0) === $s);
+?>
+--EXPECT--
+input after calls: string(3) "aaa"
+mb_encode_mimeheader() returns input: bool(true)
+mb_output_handler() returns input: bool(true)