Summary
bat --terminal-width 1 --wrap character aborts with capacity overflow (exit 101) when a line contains a character wider than the terminal (a double-width CJK char or emoji) and a background is painted on that line
(--highlight-line, or a theme/style that fills the row background). The per-line cursor advances past cursor_max (the terminal width), and the background-fill computes " ".repeat(cursor_max - cursor) — a usize subtraction that underflows to ~usize::MAX and aborts inside str::repeat.
This is the same defect class as the --style=snip width-1 panic (#3803 ).
What steps will reproduce the bug?
# 📦 is East-Asian-Wide (display width 2); --terminal-width 1 makes cursor_max 1.
$ printf '📦📦\n' | bat --highlight-line 1 --terminal-width 1 --wrap character \
--color always --paging never --theme OneHalfDark
thread 'main' panicked at library/alloc/src/slice.rs:524:23:
capacity overflow
$ echo $?
101
Trigger conditions (all clean input):
--terminal-width 1 (0 is rejected by arg parsing; width ≥ the char's display width is fine),
- character wrapping (
--wrap character), with colored output (--color=always or a real terminal),
- a background painted on the line —
--highlight-line <that line>, or any theme/style that fills the row background,
- an input line containing a character whose display width exceeds the terminal width (a double-width CJK char or emoji; ASCII never triggers it).
What did you expect to happen instead?
A terminal narrower than a single glyph should clamp the background fill to empty, not underflow and abort — mirroring the clamping bat already applies elsewhere.
Root cause
InteractivePrinter::print_line (src/printer.rs), wrapping branch. cursor_max is the terminal width (src/printer.rs:692: let mut cursor_max: usize = self.config.term_width;) and cursor accumulates each chunk's display width. When a chunk is wider than the remaining width, cursor overshoots cursor_max, and the end-of-line background fill (src/printer.rs:934) underflows:
if let Some(background_color) = background_color {
let ansi_style = Style { background: to_ansi_color(background_color, self.config.true_color), ..Default::default() };
write!(handle, "{}", ansi_style.paint(" ".repeat(cursor_max - cursor)))?; // :934
}
cursor_max - cursor = 1 - 2 underflows usize → " ".repeat(~usize::MAX) aborts with capacity overflow. (The sibling printer.rs:795, the wrapping cursor advance itself, is the same tiny-width/wide-char class.)
Environment
bat 0.26.1 (commit af1f53d9), rustc 1.90 (stable), Ubuntu 20.04 x86_64
Summary
bat --terminal-width 1 --wrap characteraborts withcapacity overflow(exit 101) when a line contains a character wider than the terminal (a double-width CJK char or emoji) and a background is painted on that line(
--highlight-line, or a theme/style that fills the row background). The per-line cursor advances pastcursor_max(the terminal width), and the background-fill computes" ".repeat(cursor_max - cursor)— ausizesubtraction that underflows to ~usize::MAXand aborts insidestr::repeat.This is the same defect class as the
--style=snipwidth-1 panic (#3803 ).What steps will reproduce the bug?
Trigger conditions (all clean input):
--terminal-width 1(0is rejected by arg parsing; width ≥ the char's display width is fine),--wrap character), with colored output (--color=alwaysor a real terminal),--highlight-line <that line>, or any theme/style that fills the row background,What did you expect to happen instead?
A terminal narrower than a single glyph should clamp the background fill to empty, not underflow and abort — mirroring the clamping bat already applies elsewhere.
Root cause
InteractivePrinter::print_line(src/printer.rs), wrapping branch.cursor_maxis the terminal width (src/printer.rs:692: let mut cursor_max: usize = self.config.term_width;) andcursoraccumulates each chunk's display width. When a chunk is wider than the remaining width,cursorovershootscursor_max, and the end-of-line background fill (src/printer.rs:934) underflows:cursor_max - cursor=1 - 2underflowsusize→" ".repeat(~usize::MAX)aborts withcapacity overflow. (The siblingprinter.rs:795, the wrapping cursor advance itself, is the same tiny-width/wide-char class.)Environment
bat
0.26.1(commitaf1f53d9), rustc 1.90 (stable), Ubuntu 20.04 x86_64