-
fix(core): prevent TUI buffer overflow panics in dependency view scrollbar rendering (#32292)
发布于
2025-08-11 20:08:09 +00:00 Current Behavior
The TUI dependency view experiences buffer overflow panics when
rendering scrollbars on small or constrained terminal sizes. Users
encounter crashes with messages like "index outside of buffer" at
various coordinates such as (134, 37), (86, 0), (107, 0), etc.Expected Behavior
The TUI should handle all terminal sizes gracefully without crashing,
maintaining proper visual rendering of scrollbars and padding elements
while staying within buffer boundaries.Key Code Changes
The fix adds clean, reusable helper functions for bounds checking to
prevent buffer overflows:1. New Helper Functions (lines 216-239):
/// Check if a rectangle fits within buffer boundaries fn fits_in_buffer(area: &Rect, buf: &Buffer) -> bool { area.x + area.width <= buf.area().width && area.y < buf.area().height } /// Create a safe rectangle clamped to buffer boundaries fn clamp_to_buffer(area: Rect, buf: &Buffer) -> Option<Rect> { if area.width == 0 || area.height == 0 { return None; } let safe_area = Rect { x: area.x, y: area.y, width: area.width.min(buf.area().width.saturating_sub(area.x)), height: area.height.min(buf.area().height.saturating_sub(area.y)), }; if safe_area.width > 0 && safe_area.height > 0 { Some(safe_area) } else { None } }2. Simplified Scrollbar Bounds Check (lines 478-481):
// Render scrollbar with bounds checking if let Some(safe_scrollbar_area) = Self::clamp_to_buffer(outer_area, buf) { scrollbar.render(safe_scrollbar_area, buf, &mut state.scrollbar_state); }3. Cleaner Padding Validation (lines 241-286):
fn render_scrollbar_padding(outer_area: Rect, buf: &mut Buffer, style: Style) { const PADDING_WIDTH: u16 = 2; const RIGHT_MARGIN: u16 = 3; const WIDTH_PADDING: u16 = 2; const TOTAL_WIDTH: u16 = PADDING_WIDTH + RIGHT_MARGIN + WIDTH_PADDING; // Early exit if area is too small or has no height if outer_area.width < TOTAL_WIDTH || outer_area.height == 0 { return; } // Use helper function for bounds checking if Self::fits_in_buffer(&top_area, buf) { // render top padding } if Self::fits_in_buffer(&bottom_area, buf) { // render bottom padding } }Key improvements:
- Reduced complexity: Scrollbar bounds checking went from 11 lines
to 3 lines - Reusable helpers:
fits_in_buffer()andclamp_to_buffer()can
be used throughout the codebase - Named constants: Replaced magic numbers with descriptive constants
- Clear intent: Function names clearly express what the code does
Related Issue(s)
This fixes buffer overflow panics that occur when the terminal user
interface attempts to render scrollbar widgets and padding outside the
available buffer boundaries, particularly on smaller terminal sizes or
when the terminal is resized.The fix adds minimal bounds checking to:
- Scrollbar rendering: Validates the scrollbar area fits within
buffer boundaries before rendering - Padding rendering: Ensures top/bottom padding areas don't extend
beyond buffer limits
Comprehensive test coverage added: 10 unit tests covering all edge
cases including the specific problematic buffer dimensions that
previously caused panics (45×30, 76×30, 104×30, 135×37, etc.).Tested across multiple terminal buffer sizes and confirmed no more
buffer overflow panics while maintaining correct scrollbar
functionality.下载附件
- Reduced complexity: Scrollbar bounds checking went from 11 lines