Skip to content

fix(mermaid-text): handle multi-byte (non-ASCII) input safely - #30

Closed
vshylov wants to merge 1 commit into
leboiko:masterfrom
vshylov:fix/mermaid-text-non-ascii-parsing
Closed

vshylov wants to merge 1 commit into
leboiko:masterfrom
vshylov:fix/mermaid-text-non-ascii-parsing

Conversation

@vshylov

@vshylov vshylov commented Jul 15, 2026

Copy link
Copy Markdown

Summary

mermaid-text used byte offsets where character offsets are expected in the parsers, producing two failures on non-ASCII (e.g. Cyrillic — but any multi-byte UTF-8) diagram text. This fixes both. Closes #29.

Changes

  • Panic → safe in strip_keyword_prefix (parser/common.rs). It sliced line[..keyword.len()] at a byte index before checking the match and without a char-boundary check, so a multi-byte line whose byte at keyword.len() fell inside a character panicked in the sequence/state parsers (participant Оркестратор, loop до …, alt …/else …). Fixed with str::get(..len) (returns None instead of panicking on a non-boundary index) + as_bytes().get(len) for the trailing-whitespace check (preserves the original length guard and ASCII-fast path).
  • Silent corruption → fixed in try_consume_pipe_label (parser/flowchart.rs). It documented a char count but returned a byte offset; the tokenizer walks a Vec<char> and advances the char cursor by it, so a multi-byte edge label over-advanced by byte_len − char_len and ate the start of the next node token — A[Start] -->|да| B[End] produced label [End] with the target node text lost. Fixed by returning chars().count().
  • Same fix applied to the compact-arrow form (-. label .->, == label ==>) in try_consume_inline_compact_arrow, which had the identical 2 + end + 3 byte-as-char-count pattern.
  • Regression tests for all three + a crate-local CHANGELOG entry.

The change is minimal and behaviour-preserving for ASCII input; the two flowchart tests differ from the passing ASCII cases only in the edge label.

Test Plan

  • Existing tests pass (cargo test --workspace — full mermaid-text suite green, 0 failures; no snapshot churn)
  • New tests added: non_ascii_lines_do_not_panic (sequence), non_ascii_pipe_label_does_not_leak_into_next_node + non_ascii_compact_arrow_label_does_not_leak (flowchart). All three fail before the fix (the sequence test panics at common.rs:45; the two flowchart tests assert the leaked bracket / lost node) and pass after.
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean
  • Manually tested on macOS / Linux — developed on Windows; not run on mac/Linux, but the change is pure &str handling with no platform surface.

Checklist

  • CHANGELOG.md entry added (crate-local crates/mermaid-text/CHANGELOG.md, [Unreleased] → Fixed)
  • No new clippy warnings
  • Documentation updated if public API changed — n/a (no public API change; internal parser helpers only)
  • Version bumped in Cargo.toml if user-visible — left to you, since you own the release/publish cadence. Happy to bump if you'd prefer it in this PR.

Linked Issue

Closes #29


Note on scope

The two issues in #29 are the ones a real corpus hit, but the underlying byte-offset-as-char-offset pattern recurs across src/parser/ (state.rs, class.rs, er.rs, gantt.rs, git_graph.rs, journey.rs, pie.rs, sankey.rs, architecture.rs). I kept this PR focused on the two confirmed user-facing bugs (+ the closely-related compact-arrow variant) so it's easy to review; a broader audit of that pattern with a couple of non-ASCII fixtures per diagram type would likely surface more. Glad to follow up if useful.

Context: I evaluated the crate for rendering LLM-generated Mermaid in a Russian-language Rust/ratatui TUI chat client — the crate's shape is a great fit and ASCII sequence diagrams look excellent, but these two bugs blocked non-ASCII use. Thanks for the crate!

Byte offsets were used where character offsets are expected in the
parsers, producing two failures on non-ASCII (e.g. Cyrillic) input.
Closes leboiko#29.

1. Panic — `strip_keyword_prefix` (parser/common.rs) sliced
   `line[..keyword.len()]` at a byte index without a char-boundary
   check. Multi-byte lines whose byte at `keyword.len()` fell inside a
   character panicked in the sequence/state parsers (e.g. a Cyrillic
   `participant`, `loop`, `alt`/`else`). Fixed with `str::get` (returns
   None instead of panicking on a non-boundary index) + `as_bytes().get`
   for the trailing-whitespace check (preserving the length guard).

2. Silent label corruption — `try_consume_pipe_label` (parser/flowchart.rs)
   documented a char count but returned a byte offset; the tokenizer walks
   a `Vec<char>` and advances the char cursor by it, so a multi-byte edge
   label over-advanced by `byte_len - char_len`, eating the start of the
   next node token (`A[Start] -->|да| B[End]` -> label `[End]`, node lost).
   Fixed by returning `chars().count()`. Same byte-as-char-count fix applied
   to the compact-arrow form in `try_consume_inline_compact_arrow`.

Adds regression tests for all three (the sequence test panics before the
fix, the two flowchart tests corrupt the label) and a crate-local
CHANGELOG entry. Full crate suite green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@vshylov vshylov closed this Jul 16, 2026
leboiko added a commit that referenced this pull request Jul 16, 2026
…#30)

@vshylov opened PR #30 with the correct fix before this maintainer wrote
and merged the superseding #31 without checking for an existing PR. The
shipped mermaid-text 0.56.1 fix is substantially their work; correct the
CHANGELOG credit from "reported by" to "reported and originally fixed by",
naming PR #30.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@leboiko leboiko reopened this Jul 16, 2026
@leboiko

leboiko commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Hi @vshylov — I owe you an apology.

You opened this PR with the correct fix on 2026-07-15, before I started. When I picked up #29 I went straight from the issue to the code and failed to check for an open PR — so I never saw #30, wrote a parallel fix in #31, and merged it, orphaning your work. You then closed this PR. That's entirely on me, and it's not the experience you'd earned after such a careful report and patch.

For the record: the fix that shipped in mermaid-text 0.56.1 is substantially your implementation — str::get in strip_keyword_prefix, and chars().count() in try_consume_pipe_label / try_consume_inline_compact_arrow are exactly what you wrote here. Taking your "broader note" seriously, a follow-up audit added a few more sites of the same class (the inline quoted arrow, a participant-alias case-folding split, and three copy-pasted clones of the keyword-strip helper in gantt/timeline/journey), which got stacked on top.

I've corrected the CHANGELOG to credit you as reported and originally fixed by @vshylov (#29, #30) rather than just the reporter (diff).

I'm reopening this briefly to acknowledge it properly, then closing it since the change is already on master — not because it wasn't good. It was good, and it was first. Thank you for the report and the patch, and sorry for the process failure. I'd genuinely welcome future PRs from you.

@leboiko

leboiko commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Closing: superseded on master by #31, which shipped your fix (mermaid-text 0.56.1) plus the audit follow-ups. Credit corrected in the CHANGELOG. Thanks again, @vshylov.

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.

Panics and silent label corruption on non-ASCII input (byte offsets used as char offsets)

2 participants