Fix capacity overflow panic wrapping a wide char at narrow terminal width - #3886
Open
vjymisal0 wants to merge 1 commit into
Open
Fix capacity overflow panic wrapping a wide char at narrow terminal width#3886vjymisal0 wants to merge 1 commit into
vjymisal0 wants to merge 1 commit into
Conversation
…idth
InteractivePrinter::print_line's wrapping branch advances `cursor` by a
chunk's full display width even when that chunk (a double-width CJK
character or emoji) is wider than `cursor_max` (the terminal width).
When a background is painted on the line (e.g. via --highlight-line),
the end-of-line fill computed `" ".repeat(cursor_max - cursor)`, which
underflows when cursor > cursor_max, aborting with "capacity overflow".
Reproduced on current master:
printf '\U0001F4E6\U0001F4E6\n' | bat --highlight-line 1 \
--terminal-width 1 --wrap character --color always \
--paging never --theme OneHalfDark
# thread 'main' panicked at src/printer.rs:961:49:
# attempt to subtract with overflow
Fix clamps the fill width with saturating_sub, matching the pattern
already used a few lines above (line 789-793) for the no-wrap branch
and the earlier width-1 snip fix (sharkdp#3804). When cursor exceeds
cursor_max, the background fill is now empty instead of underflowing.
Added a regression test (wide_char_wrap_at_terminal_width_one_with_highlight_does_not_panic)
covering the exact repro. Full integration suite (230 tests) and
clippy pass locally.
Closes sharkdp#3844
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
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 the line (--highlight-line, or a theme/style that fills the row background). Reported in #3844.Root cause
In
InteractivePrinter::print_line(src/printer.rs), the character-wrapping branch accumulatescursorby each chunk's display width. When a single character is wider than the remainingmax_width, it still gets pushed ontoline_bufand its full width added tocursorat flush time — socursorcan end up greater thancursor_max(the terminal width). The end-of-line background fill then computes:cursor_max - cursorunderflows (usize), producing a repeat count nearusize::MAX, which aborts insidestr::repeat.This is the same defect class as the
--style=snipwidth-1 panic fixed in #3804 — that fix clamps a different overflow site in the same function; this one is the last remaining unclamped subtraction of the same shape.Fix
Clamp with
saturating_sub, matching the pattern already used a few lines above (line ~789) for the no-wrap branch:When the cursor has overshot the terminal width, the background fill is now empty instead of underflowing — matching the "clamp to empty" behavior bat already uses elsewhere for this exact class of bug.
Testing
Reproduced the panic on current
masterbefore the fix:After the fix, the same command exits 0 with no panic.
Added a regression test,
wide_char_wrap_at_terminal_width_one_with_highlight_does_not_panic, covering this exact case. Ran the full integration suite (cargo test --test integration_tests): 230 passed, 0 failed. Also rancargo clippy --libwith no new warnings.Added a changelog entry under Bugfixes per CONTRIBUTING.md.
Closes #3844