Skip to content

Fix Dom\HTMLDocument::createFromFile() UTF-8 fast-path across >=3 reads - #25769

Open
hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-dom-html-utf8-multichunk
Open

hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-dom-html-utf8-multichunk

Conversation

@hohphu8

@hohphu8 hohphu8 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

In dom_decode_encode_fast_path() (ext/dom/html_document.c), when resuming with decoding_encoding_ctx->decode.status == LXB_STATUS_CONTINUE, the return value of lxb_encoding_decode_utf_8_single() is passed to lxb_encoding_encode_utf_8_single() without checking for LXB_ENCODING_DECODE_CONTINUE.

When a 3- or 4-byte UTF-8 sequence spans three or more short reads, the "need more bytes" result is treated as an invalid codepoint and replaced with U+FFFD, so valid input is corrupted (e.g. € split across three reads comes out as U+FFFD plus a stray byte).

This PR returns early with *buf_ref_ref = buf_ref when the result is LXB_ENCODING_DECODE_CONTINUE, leaving the decoder state as LXB_STATUS_CONTINUE so the next chunk resumes the sequence. A sequence truncated at EOF still becomes U+FFFD (covered by the test).

Adds ext/dom/tests/modern/html/parser/HTMLDocument_createFromFile_utf8_multichunk.phpt, which uses a user stream wrapper that returns the bytes in separate chunks. It fails before the fix and passes after.

Note: I found this through source inspection with an automated/LLM-assisted tool, then reproduced and verified it myself.

In dom_decode_encode_fast_path(), resuming with LXB_STATUS_CONTINUE did not check for LXB_ENCODING_DECODE_CONTINUE,
      passing the sentinel codepoint to lxb_encoding_encode_utf_8_single() and injecting U+FFFD when a multibyte UTF-8 character
      spans three or more short reads.
@hohphu8
hohphu8 requested a review from devnexen as a code owner November 26, 2026 04:21
Copilot AI balanced review requested due to automatic review settings November 26, 2026 04:21

This comment was marked as off-topic.

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.

2 participants