classfile: decode CONSTANT_Utf8 as modified UTF-8 - #208
Merged
Merged
Conversation
CONSTANT_Utf8 is modified UTF-8 (JVMS 4.4.7): NUL is C0 80 and a supplementary character is a surrogate pair of two 3-byte sequences. String::from_utf8 rejects both, so a class with "\0" or e.g. U+1F40D in any constant fails to parse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dlunch
enabled auto-merge (squash)
September 28, 2026 07:11
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.
CONSTANT_Utf8entries are modified UTF-8 (JVMS §4.4.7), butparse_utf8decodes them withString::from_utf8. Two encodings javac emits are rejected:C0 80ED A0..AF xx ED B0..BF xx)Any class with
"\0"or, for example, U+1F40D in a string constant fails to parse, and loading it throwsClassFormatError.Repro: compile
class A { String s = "MTR\0"; }with javac and parse the resulting class file.Fix:
decode_modified_utf8keeps thestr::from_utf8fast path for input that is already valid UTF-8. Otherwise it decodes 1–4 byte sequences into UTF-16 units and pairs them withchar::decode_utf16. So the change only widens what is accepted:String, becomes U+FFFDTests: four unit tests in
constant_pool.rs:C0 80With
String::from_utf8restored, three of the four fail.cargo test --workspace,cargo fmt --checkandcargo clippy -p classfile --all-targets -D warningspass.🤖 Generated with Claude Code