Skip to content

grep - GREP_COLORS sl=, cx= and rv are parsed but never applied #169

Open

Description

@sylvestre

GNU grep's GREP_COLORS supports whole-line colours: sl= for selected lines, cx= for
context lines, and rv, which swaps the two when -v is given. uu_grep accepts and stores
these capabilities but never uses them: with --color=always only the match colour is
emitted. Uncoloured output, exit status and match selection are unaffected.

Reproduction

Rust (incorrect)

$ printf 'foo bar\nbaz\n' | GREP_COLORS='sl=1:cx=2' ./target/release/grep --color=always -A1 foo | cat -v
^[[01;31m^[[Kfoo^[[m^[[K bar
baz
# Exit code: 0
$ printf 'foo bar\nbaz\n' | GREP_COLORS='sl=1:cx=2:rv' ./target/release/grep --color=always -v -B1 baz | cat -v
foo bar
# Exit code: 0

GNU (correct)

$ printf 'foo bar\nbaz\n' | GREP_COLORS='sl=1:cx=2' LC_ALL=C /usr/bin/grep --color=always -A1 foo | cat -v
^[[1m^[[K^[[01;31m^[[Kfoo^[[m^[[K^[[1m^[[K bar^[[m^[[K
^[[2m^[[Kbaz^[[m^[[K
# Exit code: 0
$ printf 'foo bar\nbaz\n' | GREP_COLORS='sl=1:cx=2:rv' LC_ALL=C /usr/bin/grep --color=always -v -B1 baz | cat -v
^[[2m^[[Kfoo bar^[[m^[[K
# Exit code: 0

Without rv, the last command gives ^[[1m^[[Kfoo bar^[[m^[[K in GNU (selected line uses
sl). Another example: printf 'a foo b\n' | GREP_COLORS='sl=1' grep --color=always foo
gives ^[[1m^[[Ka ^[[01;31m^[[Kfoo^[[m^[[K^[[1m^[[K b^[[m^[[K in GNU. -o output is not
affected (GNU prints only the match colour there too).

Root cause

ColorConfig::from_env (src/lib.rs:1050-1052) stores the values:

"sl" => config.selected_line = value,
"cx" => config.context_line = value,
"rv" => config.reverse_video = true,

but nothing reads selected_line, context_line or reverse_video.
OutputWriter::write_line_with_matches (src/output.rs:88-121) only wraps the match
positions in matched_selected and writes the rest of the line uncoloured.

Test

#[test]
fn grep_colors_selected_and_context_line() {
    let (_s, mut c) = ucmd();
    c.env("GREP_COLORS", "sl=1:cx=2")
        .args(&["--color=always", "-A1", "foo"])
        .pipe_in("foo bar\nbaz\n")
        .succeeds()
        .stdout_only(
            "\x1b[1m\x1b[K\x1b[01;31m\x1b[Kfoo\x1b[m\x1b[K\x1b[1m\x1b[K bar\x1b[m\x1b[K\n\
             \x1b[2m\x1b[Kbaz\x1b[m\x1b[K\n",
        );

    let (_s, mut c) = ucmd();
    c.env("GREP_COLORS", "sl=1:cx=2:rv")
        .args(&["--color=always", "-v", "-B1", "baz"])
        .pipe_in("foo bar\nbaz\n")
        .succeeds()
        .stdout_only("\x1b[2m\x1b[Kfoo bar\x1b[m\x1b[K\n");
}

Reported by Anthropic's OSS Scanner (ANT-2026-681OEJ82).

Activity

  1. added
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    gnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions