Repository navigation
Conversation
The hand-written state machine returned true at the end of the string
without checking that the last multi-byte sequence was complete, so
valid("abc\xC3") was true while compliant() and mb_check_encoding()
returned false. Code that uses valid() as a validation gate accepted
malformed UTF-8.
mb_check_encoding() performs the same checks (overlong forms,
surrogates, code points above U+10FFFF) and rejects incomplete
sequences, and symfony/polyfill-mbstring is already required.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 13, 2026
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.
Summary of Changes
StringHelper::valid()is a hand-written UTF-8 state machine. It returnedtrueat the end of the string without checking that the last multi-byte sequence was complete, so a string that ends in a truncated sequence passed:A fuzz run over 200,000 random 4-byte high-bit strings found 6,824 disagreements with
mb_check_encoding(). Code that usesvalid()as a validation gate accepted malformed UTF-8.valid()now returnsmb_check_encoding((string) $str, 'UTF-8'). That function rejects everything the state machine rejected (overlong forms, surrogates, code points above U+10FFFF, five- and six-byte sequences) and incomplete sequences as well.symfony/polyfill-mbstringis already a hard requirement. The same fuzz run finds no disagreements after the change.Testing Instructions
seedCompliantStrings, which feeds bothtestValidandtestCompliant, gets new cases: truncated 2-, 3-, 4- and 5-byte sequences at the end, an overlong encoding, a surrogate, a code point above U+10FFFF and a complete 4-byte sequence. The four truncated cases fail forvalid()before the change and pass after it.compliant()already passed all of them.vendor/bin/phpunitis green, phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on4.x-devas well (mb_trimand the other PHP 8.4 polyfill functions are not found), and #66 fixes that.Documentation Changes Required
docs/overview.md(still "TODO" on4.x-dev) gets aStringHelper::valid()section that says what is rejected.The owner's docs PR #72 rewrites
docs/overview.md. Whichever of the two is merged second has to fold this section into #72's structure and adjust thevalid()paragraph under "Notes on individual methods", which calls it a hand-written state machine. #73 also appends a section to thedocs/overview.mdstub, so the second of #73 and this PR to be merged gets a small conflict there.🤖 Generated with Claude Code