From fa00fb8ce8a9b3d139166cd7f7c225f9ae52f2d6 Mon Sep 17 00:00:00 2001 From: greymoth-jp Date: Fri, 26 Jun 2026 17:24:01 +0900 Subject: [PATCH 1/2] fix(list-languages): clamp desired_width to avoid usize underflow at tiny --terminal-width `get_languages` computed `desired_width = term_width - longest - separator.len()`. When `--terminal-width` is smaller than the longest language name (e.g. 1), this underflows `usize`: in overflow-checked builds it panics ("attempt to subtract with overflow"), and in release it wraps to ~usize::MAX, silently disabling the extension line-wrapping so `--terminal-width` is ignored for `--list-languages`. Use `saturating_sub`, mirroring the fix applied to `print_snip` in #3804 (the snip separator had the same `term_width - ...` underflow pattern). Output at normal widths is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/bin/bat/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 1d89f8ec..c8bbd368 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -152,7 +152,14 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result { let comma_separator = ", "; let separator = " "; // Line-wrapping for the possible file extension overflow. - let desired_width = config.term_width - longest - separator.len(); + // Clamp instead of subtracting: a tiny `--terminal-width` (smaller than the + // longest language name) would otherwise underflow `usize` and wrap to a huge + // value, silently disabling wrapping (and panicking in debug/overflow-checked + // builds). Mirrors the `saturating_sub` fix applied to `print_snip` in #3804. + let desired_width = config + .term_width + .saturating_sub(longest) + .saturating_sub(separator.len()); let style = if config.colored_output { Green.normal() From 8f93137246d7057f0dc08ac0a3a4cda7b752e13e Mon Sep 17 00:00:00 2001 From: greymoth Date: Fri, 26 Jun 2026 22:13:51 +0900 Subject: [PATCH 2/2] doc(changelog): add #3812 bugfix entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e98c19a..54cd8192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - Fixed syntax tests path, see #3610 (@foxfromworld) - Fix zsh tab completion word-splitting language names containing spaces (e.g. `HTML (Jinja2)`, `Apache Conf`), see #3693 (@YoshKoz) - Fix zsh tab completion offering invalid `-l` arguments (file globs, paths, hidden filenames) sourced from the second column of `--list-languages`. Closes #3735, see #3737 (@truffle-dev) +- Fix `usize` underflow in `--list-languages` when `--terminal-width` is smaller than the longest language name, see #3812 (@greymoth-jp) ## Other - Use git version of cross. See #3533 (@OctopusET)