Skip to content

fix(analytics): truncate display strings on UTF-8 char boundaries - #2789

Merged
aeppling merged 2 commits into
developfrom
fix/analytics-utf8-char-boundary-panic
Jul 3, 2026
Merged

aeppling merged 2 commits into
developfrom
fix/analytics-utf8-char-boundary-panic

Conversation

@aeppling

@aeppling aeppling commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #2787

Problem

rtk gain --history panicked when a tracked command contained non-ASCII text (Cyrillic, CJK, emoji):

thread 'main' panicked at src/analytics/gain.rs:250:54:
end byte index 22 is not a char boundary; it is inside 'Н' (bytes 21..23 of string)

Root cause

Display code in src/analytics/ truncated strings with raw byte slicing (s.len() > N then &s[..M]), which panics when the cut lands inside a multi-byte UTF-8 character. These were hand-inlined byte-based copies of the char-safe core::utils::truncate() helper that the rest of the codebase already uses. This is the 5th recurrence of this defect class (previous fixes: da486bf tee, c5ec92f git, 533894a json, #2751 parser); an audit of all string-slice sites in src/ confirmed the remaining unsafe ones were all in src/analytics/.

Every fixed site was first reproduced as a real panic against the pre-fix binary with crafted data:

Site Panic reproduced
gain.rs history command byte index 22 is not a char boundary
gain.rs failures top commands byte index 47 is not a char boundary
gain.rs failures recent commands byte index 37 is not a char boundary
gain.rs failures timestamp prefix byte index 16 is not a char boundary
session_cmd.rs session id prefix byte index 8 is not a char boundary

Fix

  • Replace the three ad-hoc truncations with the shared core::utils::truncate() (chars-based, existing helper, no new dependency).
  • Use .get(..n).unwrap_or(full) for the timestamp and session id prefix slices: valid prefix when the boundary is clean, full string instead of a panic otherwise.
  • Add a Cyrillic regression test with the exact string from the issue, next to the existing Thai/CJK/emoji truncate tests.

Verification

  • The issue repro (rtk ls -la "D:\...Ародинамический..." tracked, then rtk gain --history) now renders correctly with exit 0.
  • All 5 sites verified fixed end-to-end against the same crafted data (isolated RTK_DB_PATH databases and a sandboxed CLAUDE_CONFIG_DIR).
  • No regression: old vs new binary output on pure-ASCII data is byte-identical for gain --history, gain --failures and session, with every truncation branch exercised (ellipsis present in compared output).
  • cargo fmt --all --check, cargo clippy --all-targets (zero warnings), cargo test --all (2371 passed).

Checklist

  • Unit tests added (regression test in src/core/utils.rs)
  • Token savings: N/A (display fix, no filter change)
  • Truncated list recovery hints: N/A (CAP_* applies to filter output lists; the 10-row display limits here are pre-existing SQL LIMIT 10, unchanged)
  • Edge cases covered (Cyrillic, CJK, emoji, mid-char prefix boundaries)
  • Pre-commit gate passes
  • Manual test: rtk gain --history, rtk gain --failures, rtk session inspected

Follow-up candidate (out of scope here): enable clippy::string_slice repo-wide to prevent this defect class permanently; it requires #[allow] annotations on the ~20 verified-safe slice sites.

rtk gain --history panicked with "byte index 22 is not a char
boundary" when a tracked command contained non-ASCII text (Cyrillic,
CJK, emoji). The same byte-slicing pattern existed in the --failures
display (top commands, recent failures, timestamp prefix) and in the
session id column of rtk session, all reproduced as real panics.

Replace the ad-hoc byte-based truncations with the shared char-safe
core::utils::truncate() helper, and use .get(..n).unwrap_or() for the
timestamp and session id prefix slices so they fall back to the full
string instead of panicking. Output is byte-identical to the previous
behavior for pure-ASCII data.

Fixes #2787
Comment thread src/analytics/session_cmd.rs Resolved
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.

rtk gain --history panics on non-ASCII (Cyrillic) text — byte index not on char boundary

2 participants