fix(papergrid): saturating_sub in calculate_indent to avoid underflow panic#586
Open
momomuchu wants to merge 1 commit into
Open
fix(papergrid): saturating_sub in calculate_indent to avoid underflow panic#586momomuchu wants to merge 1 commit into
momomuchu wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
calculate_indent()panics with "attempt to subtract with overflow" when a customDimension::get_widthreturns a width smaller than the content width, because it does an uncheckedavailable - 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
Dimensionwhose width is less than the content width. They panic without the fix (at all three sites) and pass with it.cargo test -p papergridgreen, clippy and fmt clean.Note: the adjacent
rest_width = cell_width - line_widtha few lines above each site is intentionally left as-is. It is not affected by this bug:cell_widthisget_width(the max over the cell's lines) andline_widthis a single line's width, socell_width >= line_widthholds by construction.Closes #567