mirror of
https://github.com/sharkdp/bat
synced 2026-08-05 19:11:42 +00:00
Fix capacity-overflow panic in print_snip at --terminal-width 1
`InteractivePrinter::print_snip` computes the snip separator width as `term_width - panel_count - snip_left_count - title_count`. At `--terminal-width 1` the panel is disabled and `title_count == 2`, so the subtraction underflows `usize` (release has no overflow-checks, so it wraps to ~usize::MAX); `str::repeat` then aborts with "capacity overflow" whenever a snip separator is emitted (two or more disjoint line ranges, or a diff gap) — which the default style does. Use `saturating_sub` for the repeat counts so they clamp to 0 instead of underflowing. Added an integration test.
This commit is contained in:
@@ -305,6 +305,19 @@ fn line_range_multiple() {
|
||||
.stdout("line 1\nline 2\nline 4\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snip_at_terminal_width_one_does_not_panic() {
|
||||
bat()
|
||||
.arg("multiline.txt")
|
||||
.arg("--style=snip")
|
||||
.arg("--color=always")
|
||||
.arg("--terminal-width=1")
|
||||
.arg("--line-range=1:2")
|
||||
.arg("--line-range=4:4")
|
||||
.assert()
|
||||
.success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_range_multiple_with_context() {
|
||||
bat()
|
||||
|
||||
Reference in New Issue
Block a user