Skip to content

Fix capacity overflow panic for huge --line-range offset-from-end#3855

Open
koopatroopa787 wants to merge 1 commit into
sharkdp:masterfrom
koopatroopa787:fix-line-range-capacity-overflow
Open

Fix capacity overflow panic for huge --line-range offset-from-end#3855
koopatroopa787 wants to merge 1 commit into
sharkdp:masterfrom
koopatroopa787:fix-line-range-capacity-overflow

Conversation

@koopatroopa787

Copy link
Copy Markdown

Summary

bat --line-range :-N with N near usize::MAX aborted with capacity overflow (exit 101) instead of reporting an error:

$ printf 'l1\nl2\n' | bat --no-config --paging=never --line-range ':-18446744073709551614' -
thread 'main' panicked at src/controller.rs:264:62:
capacity overflow
$ echo $?
101

Two bugs combined to cause this:

  1. largest_offset_from_end() + 1 in controller.rs:262 overflowed usize when the offset was usize::MAX.
  2. VecDeque::with_capacity(usize::MAX) aborted with capacity overflow for the usize::MAX - 1 case.

Fix

  • Replace the plain + 1 with saturating_add(1) to prevent integer overflow.
  • Replace VecDeque::with_capacity with VecDeque::new() + try_reserve, which returns an error instead of panicking when the requested capacity can't be represented.

After the fix:

$ printf 'l1\nl2\n' | bat --no-config --paging=never --line-range ':-18446744073709551614' -
[bat error]: --line-range offset from end (18446744073709551614) is too large to buffer
$ echo $?
1

Normal :-N ranges continue to work unchanged.

Fixes #3845

`--line-range :-N` with N near usize::MAX aborted with "capacity
overflow" in two ways:

1. `largest_offset_from_end() + 1` overflowed usize when N == usize::MAX.
2. `VecDeque::with_capacity(usize::MAX)` panicked with capacity overflow.

Replace the plain `+ 1` with `saturating_add(1)` and the infallible
`VecDeque::with_capacity` with `try_reserve`, so an impossibly large
offset produces a clear error message instead of an abort.

Fixes sharkdp#3845
Copilot AI review requested due to automatic review settings July 20, 2026 16:24

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

bat panics (capacity overflow) on a huge --line-range offset-from-end

2 participants