From 038d800d90365726090a39b0a8693181872de03b Mon Sep 17 00:00:00 2001 From: Hohphu Date: Thu, 26 Nov 2026 11:10:09 +0700 Subject: [PATCH] Fix Dom\HTMLDocument::createFromFile() UTF-8 fast-path across >=3 reads 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. --- ext/dom/html_document.c | 5 ++ ...cument_createFromFile_utf8_multichunk.phpt | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 ext/dom/tests/modern/html/parser/HTMLDocument_createFromFile_utf8_multichunk.phpt diff --git a/ext/dom/html_document.c b/ext/dom/html_document.c index 2e4740f7372b..d365e56b19e9 100644 --- a/ext/dom/html_document.c +++ b/ext/dom/html_document.c @@ -535,6 +535,11 @@ static bool dom_decode_encode_fast_path( lxb_char_t buf[4]; lxb_char_t *buf_ptr = buf; lxb_codepoint_t codepoint = lxb_encoding_decode_utf_8_single(&decoding_encoding_ctx->decode, &buf_ref, buf_end); + if (codepoint == LXB_ENCODING_DECODE_CONTINUE) { + ZEND_ASSERT(buf_ref == buf_end); + *buf_ref_ref = buf_ref; + return true; + } if (lxb_encoding_encode_utf_8_single(&decoding_encoding_ctx->encode, &buf_ptr, buf + sizeof(buf), codepoint) > sizeof(buf)) { buf_ptr = zend_mempcpy(buf, LXB_ENCODING_REPLACEMENT_BYTES, LXB_ENCODING_REPLACEMENT_SIZE); } diff --git a/ext/dom/tests/modern/html/parser/HTMLDocument_createFromFile_utf8_multichunk.phpt b/ext/dom/tests/modern/html/parser/HTMLDocument_createFromFile_utf8_multichunk.phpt new file mode 100644 index 000000000000..b6fd0b8e17bd --- /dev/null +++ b/ext/dom/tests/modern/html/parser/HTMLDocument_createFromFile_utf8_multichunk.phpt @@ -0,0 +1,52 @@ +--TEST-- +Dom\HTMLDocument::createFromFile() streaming UTF-8 fast-path handles multi-byte sequences across >=3 short reads +--EXTENSIONS-- +dom +--FILE-- +chunks = self::$cases[$key]; + $this->index = 0; + return true; + } + + public function stream_read(int $count): string { + return $this->chunks[$this->index++] ?? ''; + } + + public function stream_eof(): bool { + return $this->index >= count($this->chunks); + } + + public function stream_stat(): array { + return []; + } +} + +stream_wrapper_register('utf8chunk', MultiChunkUtf8Stream::class); + +MultiChunkUtf8Stream::$cases = [ + 'utf3' => ['

', "\xE2", "\x82", "\xAC

"], // U+20AC across 3 reads + 'utf4a' => ['

', "\xF0", "\x9F", "\x98\x80

"], // U+1F600 across 3 reads + 'utf4b' => ['

', "\xF0", "\x9F", "\x98", "\x80

"], // U+1F600 across 4 reads + 'trunc' => ['

', "\xF0", "\x9F"], // Truncated sequence at EOF -> U+FFFD +]; + +foreach (array_keys(MultiChunkUtf8Stream::$cases) as $case) { + $dom = Dom\HTMLDocument::createFromFile("utf8chunk://$case", LIBXML_NOERROR); + echo $case, ': ', bin2hex($dom->getElementsByTagName('p')[0]->textContent), "\n"; +} +?> +--EXPECT-- +utf3: e282ac +utf4a: f09f9880 +utf4b: f09f9880 +trunc: efbfbd