Skip to content

Fix huge --line-range offset-from-end panic - #3848

Open
xenoninja wants to merge 2 commits into
sharkdp:masterfrom
xenoninja:fix/capacity-overflow-on-huge-offset
Open

Fix huge --line-range offset-from-end panic#3848
xenoninja wants to merge 2 commits into
sharkdp:masterfrom
xenoninja:fix/capacity-overflow-on-huge-offset

Conversation

@xenoninja

Copy link
Copy Markdown

When an offset-from-end line range used a very large value (for example, --line-range=:-18446744073709551614), print_file_ranges added one to the parsed usize and passed the result directly to VecDeque::with_capacity. This either triggered a capacity overflow while allocating or overflowed the addition itself.

Use saturating arithmetic for the look-ahead buffer size and let the VecDeque grow with the input instead of reserving the user-provided offset up front. This preserves existing range behavior while making memory usage depend on the actual input; offsets beyond the input now produce an empty range instead of panicking.

Add an integration test covering both usize::MAX - 1 and usize::MAX offsets.

Closes #3845.

@xenoninja
xenoninja force-pushed the fix/capacity-overflow-on-huge-offset branch from f922600 to c90ba06 Compare August 2, 2026 09:39
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

1 participant