Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/uu/unexpand/src/unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ fn parse_tabstops(s: &str) -> Result<TabConfig, ParseError> {
// Only add an extra tab stop if increment is non-zero
if let Some(inc) = increment_size.filter(|&i| i > 0) {
let last = *nums.last().unwrap();
nums.push(last + inc);
// A near-maximal last stop plus the increment can exceed `usize::MAX`.
// Reject it as too large (matching GNU) instead of overflowing, which
// aborts under overflow checks and wraps to a bogus stop otherwise.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a one line comment is probably enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

let next = last.checked_add(inc).ok_or(ParseError::TabSizeTooLarge)?;
nums.push(next);
}

if let (false, _) = nums
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ fn test_tabs_shortcut_with_too_large_size() {
new_ucmd!().arg(arg).fails().stderr_contains(expected_error);
}

#[test]
fn test_extended_tabstop_increment_overflow() {
// `--tabs=N,+M` adds one stop at `N + M`. When `N` is near `usize::MAX`,
// that sum must be rejected as too large rather than overflowing (which
// aborts under overflow checks and wraps to a bogus stop otherwise).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorry but same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

let arg = format!("--tabs={},+1", usize::MAX);
new_ucmd!()
.arg(arg)
.fails()
.stderr_contains("tab stop value is too large");
}

#[test]
fn test_is_directory() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading