Fix UB: replace unsafe String::from_utf8_unchecked#7781
Merged
Manishearth merged 1 commit intounicode-org:mainfrom Mar 16, 2026
Merged
Fix UB: replace unsafe String::from_utf8_unchecked#7781Manishearth merged 1 commit intounicode-org:mainfrom
Manishearth merged 1 commit intounicode-org:mainfrom
Conversation
…rsion A panic in a CustomTransliterator can unwind through Insertable's Drop impl without fully restoring UTF-8 validity, causing into_string() to produce undefined behavior via from_utf8_unchecked. Use checked from_utf8().expect() so this becomes a clean panic instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
a704cdd to
e2cac80
Compare
Manishearth
approved these changes
Mar 16, 2026
Member
Manishearth
left a comment
There was a problem hiding this comment.
Good call.
In general for ICU4X I would prefer us to be much more careful about unchecked indexing, it's really not worth it in most cases.
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.
with checked conversion
A panic in a CustomTransliterator can unwind through Insertable's Drop impl without fully restoring UTF-8 validity, causing into_string() to produce undefined behavior via from_utf8_unchecked. Use checked from_utf8().expect() so this becomes a clean panic instead.
This one seemed correct to me (process: hammer on Claude until it gets a good patch). Maybe there is a better way, but I think this one is ok.
Changelog
icu_experimental/transliterator: Fix UB caused in partially-written unwind states by using checked UTF-8 conversion instead of
from_utf8_unchecked