Repository navigation
fix: prevent TUI panics on multi-byte input and edge cases - #100
Conversation
- 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
left a comment
There was a problem hiding this comment.
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
-
[Low] No test for
handle_deletewith multi-byte input.
src/tui/views/settings.rs:62-70—handle_deletewas rewritten to walk forward to the next char boundary and usereplace_range, but the new tests only exercisehandle_backspace(line 590) andmove_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 totest_settings_state_multibyte_char_inputor a new test. -
[Low] No test for
move_cursor_rightwith multi-byte input.
src/tui/views/settings.rs:85-92— The CJK test (test_settings_state_split_at_safe_with_multibyte, line 622) only callsmove_cursor_lefttwice.move_cursor_righthas 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 日) aftermove_cursor_right. -
[Low] No test coverage for the
dest_ttl == 0path (#20).
src/tui/views/targets.rs:47-55—src/tui/views/targets.rshas no#[cfg(test)]module at all. Thechecked_sub(1)fix fordest_ttl == 0is untested. A small unit test constructing aTargetInfo-shaped scenario (or extracting the loss computation into a testable helper) would lock in the--rendering fordest_ttl == Some(0). Not blocking — the logic is obviously correct — but the panic path it fixes is now silently unguarded against future regressions. -
[Nit]
move_upignorestheme_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 guardmove_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. -
[Nit]
theme_index + 1could theoretically overflow iftheme_indexis stale.
src/tui/views/settings.rs:114—self.theme_index + 1 < theme_countuses wrapping addition semantics in release builds (no overflow panic), andtheme_indexis only ever set from bounded sources (move_down,SettingsState::newfromtheme_names.iter().position). Not a real risk, butself.theme_index.saturating_add(1) < theme_countwould be defensive if you want belt-and-suspenders. Optional.
Correctness verification
- Cursor bounds [0, api_key.len()]: All paths maintain the invariant.
handle_charinserts at cursor then advances bylen_utf8()(≤ 4);handle_backspacewalks back to a boundary ≤ cursor;move_cursor_leftguards>0;move_cursor_rightandhandle_deleteguard< len. Thesplit_at(api_key_cursor)call at line 340 now always receives a char-boundary offset. ✓ handle_backspaceatprev == 0: Whenapi_key_cursor == 1and byte 0 starts a multi-byte char, thewhile prev > 0loop does not execute,remove(0)removes the whole char, cursor → 0. ✓handle_deleteat end-of-string: Forward walk terminates atlen()(a valid boundary),replace_range(cursor..len, "")removes the trailing char. ✓SettingsState::newcursor: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_charfilters toc.is_ascii()(line 53), so its char-count cursor (+= 1) is unaffected and remains correct. ✓ num_targetsreorder (#18):targets.len()is now computed afterpoll_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 useschars().count() > 18+chars().take(15).collect(). Note this differs slightly fromtruncate_with_ellipsisinmain.rs:124(which uses…andmax_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.
Summary
Fixes several TUI panic paths triggered by multi-byte UTF-8 input, empty theme lists, and stale target counts.
Changes
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.chars().take(15).collect()instead of byte slicing&hostname[..15], preventing panic on multi-byte hostnames.num_targetscomputation afterpoll_add_target_resultso Tab/BackTab navigation and target-list switching use the correct count immediately after a target is added (was one tick stale).dest_ttlunderflow withchecked_subsodest_ttl == 0displays--instead of panicking or showing wrong hop data.move_downwhentheme_count == 0to prevent unsigned subtraction underflow panic.Testing
cargo clippy --all-targets -- -D warningscleanRisk
Low — localized TUI state fixes, no networking or probe-sending changes.