[php-src] master: ext/mbstring: Fix refcount info for mb_encode_mimeheader(), mb_output_handler()

From: Date: Thu, 19 Nov 2026 23:50:59 +0000
Subject: [php-src] master: ext/mbstring: Fix refcount info for mb_encode_mimeheader(), mb_output_handler()
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143943@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-11-19T18:38:59-05:00 Commit: https://github.com/php/php-src/commit/8e36329c1616e4f188f9cc2eefb605c82c8654c8 Raw diff: https://github.com/php/php-src/commit/8e36329c1616e4f188f9cc2eefb605c82c8654c8.diff 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 Changed paths: A ext/mbstring/tests/jit_returned_input_refcount.phpt M NEWS M Zend/Optimizer/zend_func_infos.h M ext/mbstring/mbstring.stub.php M ext/mbstring/mbstring_arginfo.h Diff: diff --git a/NEWS b/NEWS index 2415ca123f7f..2f4e243efe7f 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 7006bd6c8156..6a484490b346 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 af9c5cbb93ea..02e3c36d0295 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 230dddf96941..3fa13bd7bd9e 100644 --- a/ext/mbstring/mbstring_arginfo.h +++ b/ext/mbstring/mbstring_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 03c07f68bea7d7b96e6dc11f180f45663b859ed3 */ + * Stub hash: 8cfae3e99cc4ac5b55882bb962091426bb2847ca */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_language, 0, 0, MAY_BE_STRING|MAY_BE_BOOL) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, language, IS_STRING, 1, "null") 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 000000000000..a01954f7bfe4 --- /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)

« previous php.cvs (#143943) next »