VTCode icon indicating copy to clipboard operation
VTCode copied to clipboard

Panic on non-ASCII input in several byte-indexing code paths (PTY echo strip, vim j/k navigation, terminal bell notifications, project overview at startup)

Open 7jrxt42BxFZo4iAnN4CX opened this issue 3 months ago • 4 comments

Summary

Multiple components slice/truncate Rust Strings at raw byte offsets without checking UTF-8 char boundaries. Any multi-byte character (Cyrillic, CJK, emoji) whose byte happens to be at the slice offset causes a byte index N is not a char boundary panic. Four of these paths are trivially reachable by users: sending Cyrillic text to a PTY session, pressing j in vim normal mode on a line containing CJK, terminal-bell notifications, and simply starting the agent in a project whose README contains non-ASCII text beyond a length limit.

Details

  1. PTY command-echo strip — crates/codegen/vtcode-core/src/tools/pty/session.rs CommandEchoState::consume_chunk (lines 59-120): the KMP-like matcher advances index one BYTE per matched byte (index += 1, line 100), then the loop start does &text[index..] (line 65). After the first byte of a multi-byte char matches, index is mid-char → panic. Reachability verified: send_input_to_session stores CommandEchoState::new(input_text) for any valid UTF-8 input (pty/manager/session_ops.rs:55-60), and any output poll runs strip_command_echo/preview_command_echo (session.rs:325,328). Repro: write_stdin with "привет\n" into an exec PTY session, then poll output → deterministic panic in the tool call.

  2. vim j/k vertical motion — crates/codegen/vtcode-ui/src/vim/engine.rs:453-481 (move_vertical, same pattern in move_to_line): the column is tracked in BYTES and the target position is computed as (next_start + column), which lands inside a multi-byte character on the destination line. set_cursor (input_manager.rs:256-260) does no boundary validation and byte_offset_to_row_col slices line[..byte_offset - consumed] (textarea_bridge.rs:15) → panic. Repro: enter vim normal mode, cursor before a CJK character, press j where the next line contains a wide char at that byte column.

  3. Terminal-bell notification text — crates/common/vtcode-commons/src/ansi_codes.rs:183-190 (sanitize_notification_text): cleaned.truncate(MAX_LEN /*200*/) on a byte-length check; notification bodies derived from session content flow through send_message → send_terminal_bell (vtcode-core/src/notifications/mod.rs:480-484). A body where byte 200 is mid-character panics the process while ringing the terminal bell.

  4. Project overview at startup — crates/common/vtcode-commons/src/utils.rs:99-102 (extract_readme_excerpt): excerpt.truncate(max_len) on byte length; called from build_project_overview for the workspace README/QUICKSTART → the agent panics at startup in a project with a non-ASCII README longer than the byte limit.

The same byte-indexing pattern also exists in text_tools/parse_yaml.rs:82-91 (indent counted in chars, used as a byte index — runs on every model reply), tools/defuddle/mod.rs (web content truncate), and vtcode-ui/src/design/color.rs:83-85 (agent color config "红色" is exactly 6 bytes) — fixing a shared boundary-safe helper would cover all of them.

Steps to reproduce

See the per-item repros above; item 1 is fully deterministic from a single tool call.

Suggested fix

Introduce/consistently use a boundary-safe truncation helper (floor_char_boundary-style; vtcode-commons already has truncate_byte_budget and floor_char_boundary in preview.rs), and replace raw &text[n..] / truncate(n) with char-aware slicing in the listed places. Consider a clippy/ast-grep rule for String slicing with computed offsets.

Impact

Crashes (process/tool-call panics) triggered by ordinary non-ASCII content — Cyrillic/CJK users hit these constantly; item 4 aborts startup entirely. Reliability issue, no security impact.

7jrxt42BxFZo4iAnN4CX avatar Aug 28 '26 07:08 7jrxt42BxFZo4iAnN4CX

Version info: verified against 0abcebab3 (v0.147.x). Re-checked against current main (v0.149.0): all referenced files and line numbers are unchanged (git diff 0abcebab3..main is empty for every file cited above), so the report applies to 0.149.0 as-is.

7jrxt42BxFZo4iAnN4CX avatar Aug 28 '26 07:08 7jrxt42BxFZo4iAnN4CX

Fixed in commit c94945eec: the reported UTF-8 byte slicing/truncation paths now preserve character boundaries, with regression coverage for PTY echo, Vim movement, notifications, excerpts, YAML, Defuddle, and hex parsing. Focused checks and clippy pass.

vinhnx avatar Aug 29 '26 14:08 vinhnx

Hi! Commit c94945eec doesn't exist on GitHub, and no branch/PR contains the fix. Could you push it?

7jrxt42BxFZo4iAnN4CX avatar Aug 29 '26 14:08 7jrxt42BxFZo4iAnN4CX

Hi @7jrxt42BxFZo4iAnN4CX I have pushed latest changes on main, you can check

vinhnx avatar Aug 30 '26 02:08 vinhnx