Skip to content

fix(papergrid): saturating_sub in calculate_indent to avoid underflow panic#586

Open
momomuchu wants to merge 1 commit into
zhiburt:masterfrom
momomuchu:fix/calculate-indent-underflow-567
Open

fix(papergrid): saturating_sub in calculate_indent to avoid underflow panic#586
momomuchu wants to merge 1 commit into
zhiburt:masterfrom
momomuchu:fix/calculate-indent-underflow-567

Conversation

@momomuchu

@momomuchu momomuchu commented Jul 4, 2026

Copy link
Copy Markdown

calculate_indent() panics with "attempt to subtract with overflow" when a custom Dimension::get_width returns a width smaller than the content width, because it does an unchecked available - width.

The function is duplicated in three private grid modules, and all three had the same unchecked subtraction (peekable.rs:444, 985, 1861). Each now uses available.saturating_sub(width). Clamping to 0 is the correct indent when content already exceeds the reported available width.

Added regression tests driving a custom Dimension whose width is less than the content width. They panic without the fix (at all three sites) and pass with it. cargo test -p papergrid green, clippy and fmt clean.

Note: the adjacent rest_width = cell_width - line_width a few lines above each site is intentionally left as-is. It is not affected by this bug: cell_width is get_width (the max over the cell's lines) and line_width is a single line's width, so cell_width >= line_width holds by construction.

Closes #567

calculate_indent subtracted width from available without checking
that available >= width. When a custom Dimension implementation
reports a column width smaller than the actual content width,
this underflows and panics with "attempt to subtract with overflow".

Use saturating_sub in all three copies of calculate_indent
(grid_basic, grid_not_spanned, grid_spanned).

Fixes zhiburt#567
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.

papergrid panics in nushell branch

1 participant