Skip to content

Fix panic on multi-byte UTF-8 chars in ordered list escape - #72

Merged
letmutex merged 1 commit into
letmutex:mainfrom
krystophny:fix/multibyte-escape-panic
Mar 24, 2026
Merged

letmutex merged 1 commit into
letmutex:mainfrom
krystophny:fix/multibyte-escape-panic

Conversation

@krystophny

@krystophny krystophny commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

index_of_markdown_ordered_item_dot() in src/text_util.rs uses chars().enumerate() which yields character-count positions (0, 1, 2, ...), not byte offsets. The returned index is passed to String::replace_range() in escape_if_needed() which expects byte indices. When multi-byte UTF-8 characters appear before the dot, the mismatch causes an index into the middle of a multi-byte character, panicking with "byte index N is not a char boundary".

Reproducer

use htmd::convert;
let md = convert("<p>2½. Long shot</p>").unwrap();
// panics: byte index 2 is not a char boundary; it is inside '½' (bytes 1..3)

U+00BD (½) is 2 bytes in UTF-8 and passes Rust's char::is_numeric(). The dot after 2½ is at byte offset 3, but chars().enumerate() reports it at character index 2. replace_range(1..2, "\\.") then slices into the middle of the ½ character.

Fix

Switch index_of_markdown_ordered_item_dot() from chars().enumerate() to char_indices() and track the byte offset of the dot directly instead of computing it from the iterator position.

I also audited the other escape paths in escape_if_needed():

  • is_markdown_atx_heading() returns a bool, not an index -- safe
  • The -/+ unordered list check uses chars().nth(1) without byte indexing -- safe
  • The =/~/> prefix check uses insert(0, ...) -- safe

Test plan

  • Unit test for index_of_markdown_ordered_item_dot with multi-byte input ("2½. text" returns byte offset 3)
  • Integration tests for full HTML-to-markdown conversion:
    • <p>2½. Long shot</p> -- the original panic reproducer
    • <p>1é. text</p> -- accented char (not numeric, no escape needed)
    • <p>3™. text</p> -- trademark symbol (not numeric)
    • <p>10. normal</p> -- ASCII-only ordered list still escapes correctly
    • <p>2½</p> -- multi-byte numeric without dot
    • <p>日本語1. test</p> -- CJK before ordered list pattern
    • <p># héading</p> and <p>## über</p> -- ATX heading with multi-byte chars
  • All 87 existing tests pass
  • cargo clippy -- -D warnings clean
  • cargo fmt --check clean

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of multibyte UTF-8 characters in HTML-to-Markdown conversion, ensuring correct escaping of ordered-list markers and headings when preceded by non-ASCII characters.
  • Tests

    • Added comprehensive test coverage for UTF-8 character handling in markdown conversion scenarios.

index_of_markdown_ordered_item_dot() used chars().enumerate() which
yields character-count positions, not byte offsets. The returned index
was then passed to String::replace_range() which expects byte indices.
When multi-byte characters (e.g. U+00BD half-sign) preceded the dot,
the byte/char mismatch caused an out-of-bounds panic.

Switch from chars().enumerate() to char_indices() and track the byte
offset of the dot directly instead of computing it from the iterator
position.

Reproducer: converting <p>2½. Long shot</p> panicked because the
2-byte ½ character caused the dot byte offset (3) to differ from its
character index (2).

Also apply cargo fmt to fix pre-existing formatting drift in
dom_walker.rs.
@coderabbitai

coderabbitai Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9878d827-5b6a-40a6-99d3-146491e6cdfe

📥 Commits

Reviewing files that changed from the base of the PR and between 92f543d and 53b2ba8.

📒 Files selected for processing (3)
  • src/dom_walker.rs
  • src/text_util.rs
  • tests/basic_tests.rs

📝 Walkthrough

Walkthrough

The pull request refactors conditional formatting in dom_walker.rs, updates text_util.rs to compute markdown ordered-item dot positions using UTF-8 byte offsets instead of character indices, and adds comprehensive test cases in basic_tests.rs validating correct escaping behavior with multi-byte UTF-8 characters.

Changes

Cohort / File(s) Summary
UTF-8 Byte Offset Logic
src/text_util.rs
Updated index_of_markdown_ordered_item_dot to return dot position as UTF-8 byte offset via char_indices() instead of character index; added test case covering multi-byte UTF-8 input.
Formatting Refactoring
src/dom_walker.rs
Reformatted conditional assignment in walk_node from single-line let block to multi-line if expression; no behavioral changes to whitespace-trimming logic.
Test Coverage
tests/basic_tests.rs
Added 55 lines of new test cases validating HTML-to-Markdown escaping of ordered-list markers and ATX headings preceded by multi-byte UTF-8 characters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 UTF-8 bytes dance with care,
Dots and hashes float in air,
Trimming whitespace, test by test—
A hopping fix, your code's now blessed! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main fix: handling multi-byte UTF-8 characters in ordered list escape logic that was causing a panic.
Docstring Coverage ✅ Passed Docstring coverage is 84.62% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@letmutex

Copy link
Copy Markdown
Owner

Thanks for the patch!

@letmutex
letmutex merged commit ee0d3d5 into letmutex:main Mar 24, 2026
1 check passed
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.

2 participants