ls: fix panic on a color style with no attributes - #14373
Merged
sylvestre merged 1 commit intoSep 4, 2026
Conversation
get_style_code stripped the trailing reset from the rendered style with `ret.truncate(ret.len() - 4)`. A style that carries no attribute, such as the one built from `LS_COLORS='no=39'`, renders to an empty string, so the subtraction underflowed and `ls --color` panicked in debug builds. Strip the reset with strip_suffix instead, and leave the string untouched when there is none.
harshasiddartha
force-pushed
the
fix/ls-color-style-code-underflow
branch
from
September 3, 2026 18:23
543e3ee to
8e9e484
Compare
Merging this PR will improve performance by 6.11%
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
|
GNU testsuite comparison: |
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.
Fixes #14360
ls --color=alwayspanics with "attempt to subtract with overflow" in debug builds when anLS_COLORSentry yields a style that has no attributes, for example:get_style_codepainted an empty string with nu-ansi-term and then stripped the trailing\x1b[0mwithret.truncate(ret.len() - 4). A style with no attributes paints to an empty string, so there is no trailing reset to strip and the subtraction underflows onusize.Rather than painting and then stripping, this asks nu-ansi-term for the prefix directly with
nu_a_style.prefix().to_string(). That returns the escape sequence on its own, and an empty string for a style with no attributes, so there is nothing to strip and the underflow cannot happen.Style::write_prefixandwrite_suffixare gated on the sameis_plain()check, so the result is byte-identical to the old code for every style that previously worked:\x1b[07m\x1b[0mstill becomes\x1b[07m.Tested with the new
test_ls_color_empty_styleintests/by-util/test_ls.rs, which pins the exact output. Without the fix it fails with the panic and exit code 101; with the fix it passes. The other--colortests still pass (cargo test --test tests --features ls test_ls_color, 7 passed),cargo clippy -p uu_ls --all-targetsis clean, andrustfmt --checkpasses on both touched files.One thing I did not change and want to flag: with
no=39the style is normalized away entirely, so we emit no\x1b[39mfor it. That is a separate compatibility question from the panic, so I left it out of this PR. The test pins current behavior, which makes that gap easy to spot if someone fixes it later. I built and tested only thelscrate and theteststarget locally, not the full workspace.