Repository navigation
Fix panic on multi-byte UTF-8 chars in ordered list escape - #72
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe pull request refactors conditional formatting in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
Thanks for the patch! |
Summary
index_of_markdown_ordered_item_dot()insrc/text_util.rsuseschars().enumerate()which yields character-count positions (0, 1, 2, ...), not byte offsets. The returned index is passed toString::replace_range()inescape_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
U+00BD (½) is 2 bytes in UTF-8 and passes Rust's
char::is_numeric(). The dot after2½is at byte offset 3, butchars().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()fromchars().enumerate()tochar_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-/+unordered list check useschars().nth(1)without byte indexing -- safe=/~/>prefix check usesinsert(0, ...)-- safeTest plan
index_of_markdown_ordered_item_dotwith multi-byte input ("2½. text"returns byte offset 3)<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 charscargo clippy -- -D warningscleancargo fmt --checkcleanSummary by CodeRabbit
Bug Fixes
Tests