Skip to content

fix: prevent TUI panics on multi-byte input and edge cases - #100

Merged
lance0 merged 1 commit into
masterfrom
fix/tui-panic-fixes
Jul 6, 2026
Merged

lance0 merged 1 commit into
masterfrom
fix/tui-panic-fixes

Conversation

@lance0

@lance0 lance0 commented Jul 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes several TUI panic paths triggered by multi-byte UTF-8 input, empty theme lists, and stale target counts.

Changes

  • ttl fails to do anything on an DFZ BGP edge router #16 (medium): Fix API-key cursor to track byte offsets (c.len_utf8()) instead of char count, preventing panic when multi-byte UTF-8 characters (emoji, accented letters, CJK) are pasted into the PeeringDB API-key field. All cursor movement (handle_char, handle_backspace, handle_delete, move_cursor_left/right) now walks char boundaries.
  • Feature Requests + Changes, to match mtr #17 (medium): Fix hostname truncation in target-list overlay to use chars().take(15).collect() instead of byte slicing &hostname[..15], preventing panic on multi-byte hostnames.
  • Hop Display Scroll #18 (medium): Move num_targets computation after poll_add_target_result so Tab/BackTab navigation and target-list switching use the correct count immediately after a target is added (was one tick stale).
  • Weird Install Issue #20 (low): Guard dest_ttl underflow with checked_sub so dest_ttl == 0 displays -- instead of panicking or showing wrong hop data.
  • PMTUD Causes Weird Packet Loss Initially #21 (low): Guard move_down when theme_count == 0 to prevent unsigned subtraction underflow panic.

Testing

  • All 499 tests pass (including 4 new tests)
  • cargo clippy --all-targets -- -D warnings clean
  • New tests: multi-byte char input/backspace/cursor, empty theme list move_down, split_at safety with CJK characters

Risk

Low — localized TUI state fixes, no networking or probe-sending changes.

- Fix API-key cursor to track byte offsets (c.len_utf8) instead of char count,
  preventing panic when multi-byte UTF-8 chars are pasted into the settings
  API-key field (#16)
- Fix hostname truncation in target-list overlay to use chars().take() instead
  of byte slicing &[..15], preventing panic on multi-byte hostnames (#17)
- Move num_targets computation after poll_add_target_result so Tab/BackTab and
  target-list navigation use the correct count immediately after add (#18)
- Guard dest_ttl underflow with checked_sub so TTL 0 shows -- instead of
  panicking or showing wrong hop data (#20)
- Guard move_down theme_count==0 underflow that would panic (#21)

All 499 tests pass; clippy clean.

@lance0 lance0 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: PR #100 — TUI panic fixes (multi-byte input + edge cases)

Verdict: Approve with comments

The PR correctly addresses all five reported panic paths (#16, #17, #18, #20, #21). The byte-offset cursor tracking is consistent across all five cursor methods, the checked_sub and theme_count == 0 guards are sound, and hostname truncation is now char-boundary safe. The fixes are localized and low-risk. Below are the findings.

Findings

  1. [Low] No test for handle_delete with multi-byte input.
    src/tui/views/settings.rs:62-70 — handle_delete was rewritten to walk forward to the next char boundary and use replace_range, but the new tests only exercise handle_backspace (line 590) and move_cursor_left (line 595) with multi-byte chars. handle_delete's forward-walk logic is distinct from backspace and deserves its own coverage — e.g., cursor at start of a 3-byte CJK char, and cursor before a multi-byte char at end-of-string. Recommend adding a case to test_settings_state_multibyte_char_input or a new test.

  2. [Low] No test for move_cursor_right with multi-byte input.
    src/tui/views/settings.rs:85-92 — The CJK test (test_settings_state_split_at_safe_with_multibyte, line 622) only calls move_cursor_left twice. move_cursor_right has its own forward-walk loop that is not exercised with multi-byte chars. Recommend asserting cursor advances by char width (e.g., 3 bytes for 日) after move_cursor_right.

  3. [Low] No test coverage for the dest_ttl == 0 path (#20).
    src/tui/views/targets.rs:47-55 — src/tui/views/targets.rs has no #[cfg(test)] module at all. The checked_sub(1) fix for dest_ttl == 0 is untested. A small unit test constructing a TargetInfo-shaped scenario (or extracting the loss computation into a testable helper) would lock in the -- rendering for dest_ttl == Some(0). Not blocking — the logic is obviously correct — but the panic path it fixes is now silently unguarded against future regressions.

  4. [Nit] move_up ignores theme_count (pre-existing).
    src/tui/views/settings.rs:96 — move_up(&mut self, _theme_count: usize) never references the count, so it cannot underflow. The PR only needed to guard move_down. No change required; noting for completeness since the review focus asked about "other unchecked arithmetic on theme_count in the same view" — there is none.

  5. [Nit] theme_index + 1 could theoretically overflow if theme_index is stale.
    src/tui/views/settings.rs:114 — self.theme_index + 1 < theme_count uses wrapping addition semantics in release builds (no overflow panic), and theme_index is only ever set from bounded sources (move_down, SettingsState::new from theme_names.iter().position). Not a real risk, but self.theme_index.saturating_add(1) < theme_count would be defensive if you want belt-and-suspenders. Optional.

Correctness verification

  • Cursor bounds [0, api_key.len()]: All paths maintain the invariant. handle_char inserts at cursor then advances by len_utf8() (≤ 4); handle_backspace walks back to a boundary ≤ cursor; move_cursor_left guards >0; move_cursor_right and handle_delete guard < len. The split_at(api_key_cursor) call at line 340 now always receives a char-boundary offset. ✓
  • handle_backspace at prev == 0: When api_key_cursor == 1 and byte 0 starts a multi-byte char, the while prev > 0 loop does not execute, remove(0) removes the whole char, cursor → 0. ✓
  • handle_delete at end-of-string: Forward walk terminates at len() (a valid boundary), replace_range(cursor..len, "") removes the trailing char. ✓
  • SettingsState::new cursor: api_key_cursor = api_key.len() (byte length) is consistent with byte-offset semantics. Existing ASCII tests (e.g. line 421, "test_key" → 8) remain correct. ✓
  • No regression in target_input.rs: TargetInputState::handle_char filters to c.is_ascii() (line 53), so its char-count cursor (+= 1) is unaffected and remains correct. ✓
  • num_targets reorder (#18): targets.len() is now computed after poll_add_target_result (app.rs:506→510), so Tab/BackTab and target-list switching see the new count in the same tick. ✓
  • Hostname truncation (#17): Only one truncation site in targets.rs (lines 130-135); now uses chars().count() > 18 + chars().take(15).collect(). Note this differs slightly from truncate_with_ellipsis in main.rs:124 (which uses … and max_len-1), but the two serve different columns and the behavior matches the original "...". suffix intent. ✓

Overall this is a clean, well-scoped set of panic fixes. The main gap is test coverage for the two rewritten forward-walking methods (handle_delete, move_cursor_right) — worth adding before merge but not blocking given CI passes and the logic is correct.

@lance0
lance0 merged commit fb3a5b2 into master Jul 6, 2026
5 checks passed
@lance0
lance0 deleted the fix/tui-panic-fixes branch July 6, 2026 13:49
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.

1 participant