From 699506206676a499021ba6691faf99028c315561 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Fri, 27 Nov 2026 07:06:08 -0500 Subject: [PATCH] Zend: Map the scanner position through both filters on encoding switch zend_multibyte_yyinput_again() re-converts the whole script with the new input filter, but kept the cursor at the byte offset it had in the old filtered buffer. When the code before declare(encoding=...) converts to a different length under the new encoding, scanning resumed before or after the end of the declare, which caused parse errors, silently skipped code, or reads past the end of the new buffer. Map the cursor back to the source offset through the old filter and forward through the new one. The backward search, shared with zend_get_scanned_file_offset(), becomes a bisection for the shortest source prefix whose conversion starts with the scanned bytes: it needs O(log n) conversions instead of one per byte, and it ends on a character boundary even when a truncated character converts to a substitute of a different length. --- NEWS | 3 + .../multibyte/multibyte_encoding_010.phpt | 67 ++++++++++++++++ Zend/zend_language_scanner.l | 80 +++++++++++++++---- 3 files changed, 133 insertions(+), 17 deletions(-) create mode 100644 Zend/tests/multibyte/multibyte_encoding_010.phpt diff --git a/NEWS b/NEWS index 73c9b4fefad7..4845df74b57c 100644 --- a/NEWS +++ b/NEWS @@ -43,6 +43,9 @@ PHP NEWS . Fix OSS-Fuzz #588264417. (ndossche) . Fixed bug GH-24701 (Use-after-free on typed trait constants during opcache preload). (sanklamm, lazerg) + . Fixed declare(encoding=...) with zend.multibyte resuming at the wrong + position when the code before it converts to a different length. + (Ilia Alshanetsky) - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) diff --git a/Zend/tests/multibyte/multibyte_encoding_010.phpt b/Zend/tests/multibyte/multibyte_encoding_010.phpt new file mode 100644 index 000000000000..81e2b3b2212d --- /dev/null +++ b/Zend/tests/multibyte/multibyte_encoding_010.phpt @@ -0,0 +1,67 @@ +--TEST-- +Zend Multibyte and declare(encoding=...) switching to an encoding of different width +--EXTENSIONS-- +mbstring +tokenizer +--INI-- +zend.multibyte=1 +zend.script_encoding=ISO-8859-1 +internal_encoding=UTF-8 +--FILE-- + is_array($t) ? $t[1] : $t, $tokens), -6)), PHP_EOL; + +try { + token_get_all('getMessage(), PHP_EOL; +} + +var_dump(include_code(" +--CLEAN-- + +--EXPECT-- +string(5) "café" +string(5) "café" +string(2) "ok" +;| |echo| |'café'|; +ParseError: syntax error, unexpected token "??" +string(5) "café" +string(3) "€" +string(2) "ok" +string(5) "café" +int(94) +int(94) +int(114) +int(114) +string(2) "ok" diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index 6672c4e2b99b..32126a69413e 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -759,23 +759,46 @@ ZEND_API void zend_prepare_string_for_scanning(zval *str, zend_string *filename) } +static size_t zend_multibyte_script_offset(size_t filtered_offset) +{ + size_t low = 0, high = SCNG(script_org_size) + 1; + + while (low < high) { + size_t mid = low + (high - low) / 2, length = 0; + unsigned char *p = NULL; + + if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), mid)) { + return (size_t)-1; + } + if (length >= filtered_offset && (!filtered_offset || !memcmp(p, SCNG(yy_start), filtered_offset))) { + high = mid; + } else { + low = mid + 1; + } + efree(p); + } + return low > SCNG(script_org_size) ? (size_t)-1 : low; +} + +static size_t zend_multibyte_filtered_length(size_t offset) +{ + size_t length = offset; + + if (SCNG(input_filter)) { + unsigned char *p = NULL; + if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) { + return (size_t)-1; + } + efree(p); + } + return length; +} + ZEND_API size_t zend_get_scanned_file_offset(void) { size_t offset = SCNG(yy_cursor) - SCNG(yy_start); if (SCNG(input_filter)) { - size_t original_offset = offset, length = 0; - do { - unsigned char *p = NULL; - if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) { - return (size_t)-1; - } - efree(p); - if (length > original_offset) { - offset--; - } else if (length < original_offset) { - offset++; - } - } while (original_offset != length); + offset = zend_multibyte_script_offset(offset); } return offset; } @@ -863,9 +886,27 @@ void highlight_string(zend_string *str, zend_syntax_highlighter_ini *syntax_high ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding) { - size_t length; + const zend_encoding *new_encoding = SCNG(script_encoding); + zend_encoding_filter new_input_filter = SCNG(input_filter); + size_t length, offset; unsigned char *new_yy_start; + if (old_input_filter) { + SCNG(script_encoding) = old_encoding; + SCNG(input_filter) = old_input_filter; + offset = zend_multibyte_script_offset(SCNG(yy_cursor) - SCNG(yy_start)); + SCNG(script_encoding) = new_encoding; + SCNG(input_filter) = new_input_filter; + } else { + offset = SCNG(yy_cursor) - SCNG(script_org); + } + + if (offset <= SCNG(script_org_size)) { + offset = zend_multibyte_filtered_length(offset); + } else { + offset = (size_t)-1; + } + /* convert and set */ if (!SCNG(input_filter)) { if (SCNG(script_filtered)) { @@ -887,9 +928,14 @@ ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter SCNG(script_filtered_size) = length; } - SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start)); - SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start)); - SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start)); + if (offset > length) { + zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected " + "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding))); + } + + SCNG(yy_cursor) = new_yy_start + offset; + SCNG(yy_marker) = SCNG(yy_cursor); + SCNG(yy_text) = SCNG(yy_cursor); SCNG(yy_limit) = new_yy_start + length; SCNG(yy_start) = new_yy_start;