Skip to content

Zend: Map the scanner position through both filters on encoding switch - #25875

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/zend-multibyte-yyinput-again-offset
Open

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/zend-multibyte-yyinput-again-offset

Conversation

@iliaal

@iliaal iliaal commented Nov 28, 2026

Copy link
Copy Markdown
Member

With zend.multibyte on, declare(encoding=...) re-converts the script with the new encoding but keeps the scanner cursor at its byte offset in the old converted buffer. When the code before the declare converts to a different length, scanning resumes at the wrong place, so valid scripts fail to parse or skip code, and a shorter conversion such as UTF-32 leaves the cursor past the end of the buffer. Map the cursor back to a 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, so it stops on a character boundary even when a truncated unit converts to a substitute.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant