Repository navigation
Throw on any invalid UTF-8 in HTML instead of looping forever - #873
Draft
gravitypdf-bot wants to merge 1 commit into
Draft
gravitypdf-bot wants to merge 1 commit into
gravitypdf-bot wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: the fix and its test are in. The changelog entry and the pre-merge gates are still to come.
The bug
$mpdf->WriteHTML("<p>\xE2"), HTML ending in a cut-off UTF-8 sequence, never returns.On invalid input,
purify_utf8()loops and asksiconvwhere the bad byte is.iconvreturnsfalseand reports either "illegal character" or "incomplete multibyte character". Only the first message is checked. On the second,strlen(false)makes$pos0,substr($html, 0)changes nothing, and the loop repeats, logging an empty entry each time. A logger that keeps its entries runs out of memory instead of hanging.Reading the message through
error_get_last()causes a second hang: an error handler that returns true keeps the notice out of it, so "illegal" goes unseen too.Inputs that hung
Measured on PHP 7.4, 8.2, 8.3 and 8.5 with libiconv on macOS, with the same results on all four:
\xF4, such as<p>\xF5.\x80at the start, such as<p>\xE2 ok</p>or\x80.A string made up only of a cut-off sequence, such as
\xE2, did not hang, because the loop stepped over the leading high bytes.The fix
The loop is gone, and
iconvwith it.purify_utf8()finds the first bad byte by comparing the input with its mbstring round trip, logs it and throws. Nothing repeats, so there is nothing left to hang.$startwas never read, and on the versions probed the loop body ran at most a couple of times with a useful result.Public messages
Invalid input characters. Did you set $mpdf->in_charset properly?, the message most invalid input already got.HTML contains invalid UTF-8 character(s). See log for further detailsis removed. It was thrown for inputs such as\xE2,\xF4\x90\x80\x80and a five-byte sequence, which now get the message above.[[#226]]log entry is now written for all invalid input, where before it was written only on the second message's path. It also now counts\x80as a bad byte, which> 128skipped.purify_utf8_text()andignore_invalid_utf8do not use this loop and are unchanged.Test
PurifyUtf8Testruns each case in its own process throughFixtures/purify-utf8.php, withset_time_limit(5), so a regression fails instead of stalling CI. The limit counts CPU time on Linux and wall-clock time on Windows.Against the unfixed code all 18 tests failed in about 11 seconds, the truncated cases by exhausting memory in the recording logger. With the fix they pass on PHP 8.5.
Overlap with #871
#871 replaces the round trip in
is_utf8()withmb_check_encoding()and rewrites this loop's condition. The conflict is on thewhileline: keep this PR's version with no loop, and #871'sis_utf8(). The round trip here still finds the offset after that.Not yet done
composer test,composer csand PHPStanPHP 5.6 cannot be run locally and is left to CI.