Skip to content

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

Closed
iliaal wants to merge 7 commits into
PHP-8.4from
fix/zend-multibyte-yyinput-again-offset
Closed

iliaal wants to merge 7 commits into
PHP-8.4from
fix/zend-multibyte-yyinput-again-offset

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner

With zend.multibyte on, declare(encoding=...) re-converts the whole script with the new encoding but kept 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: valid scripts fail to parse or skip bytes, and a shorter conversion such as UTF-32 puts the cursor past the end of the buffer, which token_get_all(..., TOKEN_PARSE) turns into a heap over-read.

The cursor is now mapped back to a source offset through the old filter and forward through the new one. The backward search it shares with zend_get_scanned_file_offset() is now a bisection for the shortest source prefix whose conversion starts with the scanned bytes, so it terminates when a truncated UTF-16 unit converts to a multi-byte substitute and costs a logarithmic number of conversions instead of one per byte.

Companion to #362, which pads the converted buffer. Found while validating #362; an independent review fuzzed 1,273 scripts across encodings and substitute settings with no regressions against PHP-8.4. multibyte_encoding_010.phpt fails before and passes after under ASAN.

iliaal and others added 7 commits November 27, 2026 12:59
DCE rewrites a dead instruction whose operand still needs freeing into a
FREE, but kept the instruction's extended_value. After (float) and (array)
casts and empty() it has bit 0 set, which ZEND_HANDLE_EXCEPTION reads as
ZEND_FREE_ON_RETURN: when a destructor run by that FREE throws, the throw
point moves by the FREE's op2, which is (uint32_t)-1, and
cleanup_live_vars() frees the operand a second time.

Closes phpGH-25836
The DB-Lib error and message handlers estrdup() server and library text
into the connection's pdo_dblib_err. For a persistent connection, errors
raised through PDO::exec() and the transaction and lastInsertId() calls
stay there until the next statement execute, so a request that ends after
such an error leaves pointers into the reset request heap. The next
request efree()s them from the message or error handler or the cursor
closer, and module shutdown does from the handle closer. Free them from
persistent_shutdown once the last PDO object using the connection is
released.

Closes phpGH-25159
…#25719)

Dereference the options element of the context parameters before checking
its type, so stream_context_create() and stream_context_set_params()
accept references to valid options arrays. References to other types
still raise a TypeError.

Co-authored-by: Gina Peter Banyard <girgias@php.net>

Closes php#25719
…down.

Objects outliving RSHUTDOWN dropped an empty archive at refcount 0
through the already destroyed fname map, while another object still
held it. The archive is now left to its last holder.

Fix phpGH-25722
Close phpGH-25728
socket_addrinfo_lookup(), socket_bind(), socket_connect(), socket_sendto()
and socket_sendmsg() would otherwise resolve only the part before the NUL
byte.

Closes phpGH-25774
When zend_string_safe_falloc() fails under memory_limit,
php_str_to_str_i_ex() returned NULL without releasing the lowercased needle.

Closes phpGH-25848
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.
@iliaal
iliaal force-pushed the fix/zend-multibyte-yyinput-again-offset branch from d7a8a6a to 6995062 Compare November 28, 2026 15:26
@iliaal

iliaal commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream as php/php-src phpGH-25875.

@iliaal iliaal closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants