Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arithmetic overflow in LineRange::parse_range #2698

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- Fix `more` not being found on Windows when provided via `BAT_PAGER`, see #2570, #2580, and #2651 (@mataha)
- Switched default behavior of `--map-syntax` to be case insensitive #2520
- Updated version of `serde_yaml` to `0.9`. See #2627 (@Raghav-Bell)
- Fixed arithmetic overflow in `LineRange::from`, see #2674 (@skoriop)

## Other

- Output directory for generated assets (completion, manual) can be customized, see #2515 (@tranzystorek-io)
Expand Down
9 changes: 8 additions & 1 deletion src/line_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl LineRange {
let more_lines = &line_numbers[1][1..]
.parse()
.map_err(|_| "Invalid character after +")?;
new_range.lower + more_lines
new_range.lower.saturating_add(*more_lines)
} else if first_byte == Some(b'-') {
// this will prevent values like "-+5" even though "+5" is valid integer
if line_numbers[1][1..].bytes().next() == Some(b'+') {
Expand Down Expand Up @@ -128,6 +128,13 @@ fn test_parse_plus() {
assert_eq!(50, range.upper);
}

#[test]
fn test_parse_plus_overflow() {
let range = LineRange::from(&format!("{}:+1", usize::MAX)).expect("Shouldn't fail on test!");
assert_eq!(usize::MAX, range.lower);
assert_eq!(usize::MAX, range.upper);
}

#[test]
fn test_parse_plus_fail() {
let range = LineRange::from("40:+z");
Expand Down