Skip to content

grep: handle malformed color environment values - #139

Merged
lhecker merged 2 commits into
uutils:mainfrom
SichenLiang:grep-malformed-colors
Oct 13, 2026
Merged

lhecker merged 2 commits into
uutils:mainfrom
SichenLiang:grep-malformed-colors

Conversation

@SichenLiang

@SichenLiang SichenLiang commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Ignore a malformed GREP_COLOR without printing the deprecation warning. Valid values still work and still produce the warning.
When parsing GREP_COLORS, stop at the first malformed entry or empty key, but keep entries parsed before it.

$ printf 'hit one\n' |
    env -u GREP_COLORS LC_ALL=C GREP_COLOR=4x2 \
    grep --color=always hit 2>&1 | cat -v

GNU prints the match in its default color without a warning. uutils previously printed a warning and used ^[[4x2m as the escape sequence.
Fixes #135

Ignore malformed `GREP_COLOR` values without warning.
Stop parsing `GREP_COLORS` at a malformed entry or empty key while
keeping entries parsed before it. Do not discard the whole variable
when it contains invalid UTF-8.
@codspeed

codspeed Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 3 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing SichenLiang:grep-malformed-colors (71fe9f6) with main (b68f81c)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/lib.rs Outdated
Comment on lines +148 to +149
// Decode lossily to preserve valid entries; replacement characters cannot
// match capability names or pass color-value validation.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of trying to recover a malformed non-UTF8 GREP_COLORS env variable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much, GNU does keep valid entries around a bad byte, but this isn't a realistic environment value and isn't worth the extra handling. I dropped that part so a non-UTF-8 GREP_COLORS is ignored as a whole again.

Keep the existing behavior for non-UTF-8 values and treat the whole
variable as unset. Recovering valid entries around a bad byte is not
worth the extra handling here.

@lhecker lhecker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@lhecker
lhecker merged commit 01fb6c3 into uutils:main Oct 13, 2026
22 checks passed
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.

grep: malformed color codes are written into terminal escape sequences

2 participants