Skip to content

Fix panic when wrapping a wide char with a background at tiny width - #3890

Open
nagendramohan wants to merge 3 commits into
sharkdp:masterfrom
nagendramohan:fix/wide-char-wrap-panic-3844
Open

Fix panic when wrapping a wide char with a background at tiny width#3890
nagendramohan wants to merge 3 commits into
sharkdp:masterfrom
nagendramohan:fix/wide-char-wrap-panic-3844

Conversation

@nagendramohan

Copy link
Copy Markdown

bat aborts with 'capacity overflow' running e.g.

printf '📦📦\n' | bat --terminal-width 1 --wrap character \
    --highlight-line 1 --color always

When a character is wider than the terminal (a double-width CJK char or emoji) and a background is painted on the line, the per-line cursor advances past cursor_max, and the background fill computes " ".repeat(cursor_max - cursor) — a usize subtraction that underflows and aborts inside str::repeat.

Clamp the fill width with saturating_sub so a terminal narrower than a single glyph yields an empty fill instead of underflowing, mirroring the clamping bat already applies elsewhere.

Adds a regression test (verified failing before the fix) and CHANGELOG entry. Closes #3844.

Fixes #3844.

bat aborts with capacity overflow on e.g.:

printf '📦📦\n' | bat --terminal-width 1 --wrap character --highlight-line 1 --color always

When a character is wider than the terminal (a double-width CJK char or emoji) and a background is painted on the line, the per-line cursor advances
past cursor_max, so the background fill " ".repeat(cursor_max - cursor) (src/printer.rs) does a usize subtraction that underflows and aborts inside
str::repeat.

Fix: clamp the fill width with saturating_sub, so a terminal narrower than a single glyph yields an empty fill instead of underflowing — mirroring the clamping bat already applies elsewhere. Same defect class as the --style=snip width-1 panic (#3803).

Adds a regression test (verified failing before the fix) and a CHANGELOG entry. cargo fmt --check, cargo clippy, and the full integration suite (255 tests) all pass locally.

bat aborts with 'capacity overflow' running e.g.

    printf '📦📦\n' | bat --terminal-width 1 --wrap character \
        --highlight-line 1 --color always

When a character is wider than the terminal (a double-width CJK char or
emoji) and a background is painted on the line, the per-line cursor
advances past cursor_max, and the background fill computes
" ".repeat(cursor_max - cursor) — a usize subtraction that underflows
and aborts inside str::repeat.

Clamp the fill width with saturating_sub so a terminal narrower than a
single glyph yields an empty fill instead of underflowing, mirroring the
clamping bat already applies elsewhere.

Adds a regression test (verified failing before the fix) and CHANGELOG
entry. Closes sharkdp#3844.

Signed-off-by: Nagendra Mohan <nagendramohan1990@gmail.com>
Signed-off-by: Nagendra Mohan <nagendramohan1990@gmail.com>
Signed-off-by: Nagendra Mohan <nagendramohan1990@gmail.com>
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 --terminal-width 1 wrapping a double-width char with a background

1 participant