Skip to content

Comments

fix mutf-8 codec#40

Merged
hayanesuru merged 2 commits intomainfrom
fix-mutf8
Feb 15, 2026
Merged

fix mutf-8 codec#40
hayanesuru merged 2 commits intomainfrom
fix-mutf8

Conversation

@hayanesuru
Copy link
Owner

@hayanesuru hayanesuru commented Feb 15, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Stricter validation and error reporting for invalid modified UTF-8 continuation and overlong/null-encoding sequences.
    • Improved handling and correct conversion of UTF-16 surrogate pairs to UTF-8, ensuring buffered data is emitted in the right order.

@coderabbitai
Copy link

coderabbitai bot commented Feb 15, 2026

📝 Walkthrough

Walkthrough

Adds explicit handling and validation for 0xC0 and 0xC2..=0xDF MUTF-8 sequences, updates continuation-byte checks, and extends 0xED surrogate handling to compute code points and emit corresponding 4-byte UTF-8 sequences while adjusting buffer/index management.

Changes

Cohort / File(s) Summary
MUTF-8 decoding logic
haya_mutf8/src/lib.rs
Introduced a distinct 0xC0 branch that enforces 0xC0 0x80 as NUL, added a 0xC2..=0xDF branch with explicit continuation-byte validation and length updates in decode_mutf8_len and decode_mutf8. Invalid continuations now produce errors or are skipped as implemented.
Surrogate pair handling (0xED)
haya_mutf8/src/lib.rs
Extended the 0xED branch to compute the Unicode code point from surrogate pairs, flush buffered bytes before writing the derived 4-byte UTF-8 sequence, advance indices by 6, and reset start position accordingly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰
I nibble bytes where rules were blurred,
0xC0 now hops in single word,
Surrogates leap, four bytes sing true,
A mittened patch, decoded new. 🥕

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-mutf8

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@haya_mutf8/src/lib.rs`:
- Around line 139-148: In the 0xC0 match arm that inspects let [sec, ref rest @
..] = bytes[..], add an explicit error branch when sec != 0x80 so the function
returns Err(Error) instead of silently accepting an invalid continuation byte;
update the logic around variables bytes, sec, rest and len in that arm to only
increment len and advance bytes when sec == 0x80, otherwise return Err(Error) to
enforce the MUTF-8 rule for the 0xC0 0x80 sequence.
- Around line 209-222: The 0xC0 match arm is advancing index before writing
which causes the 0xC0 0x80 sequence to be copied; in the match arm handling
MUTF-8 null (symbols: bytes, index, start, w.write, w.write_byte) move the index
+= 2 to after the w.write call so you first write
bytes.get_unchecked(start..index) (which excludes the 0xC0), then increment
index by 2, call w.write_byte(b'\0'), and set start = index; keep the same error
path when sec != 0x80.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@haya_mutf8/src/lib.rs`:
- Around line 225-234: The 0xC2..=0xDF branch currently validates the
continuation byte using `sec as i8 >= -64`; replace that check with the same
bitmask style used elsewhere (e.g., `byte & 0xC0 == 0x80`) to match the
`0xE0..=0xEF` branch for consistent readability — locate the `0xC2..=0xDF` match
arm (uses `sec` from `bytes.get(index + 1)`) and update its validation condition
while keeping the same early `Err(Error)` return and `index += 2` behavior.

@hayanesuru hayanesuru merged commit 30fc111 into main Feb 15, 2026
2 checks passed
@hayanesuru hayanesuru deleted the fix-mutf8 branch February 15, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant