Skip to content

grep - . and \S match a byte that is not valid UTF-8 #194

Open

Description

@sylvestre

In a UTF-8 locale a byte that does not start a valid character (an encoding error such as a
lone \377 or \200, or an overlong sequence) is not a character, so GNU grep never lets .
or \S match it. uu_grep searches the raw bytes with Oniguruma's UTF-8 encoding, which
happily steps over such a byte as if it were one character. Patterns built from ./.*/\S
then select lines GNU does not select (and -v drops lines GNU keeps).

uu_grep already assumes UTF-8 input everywhere (README, "Known Issues"), so this is wrong
even under its own model, independent of locale support. PR #108 mentions the \S variant in
passing; there is no issue for it.

Exposed by the GNU tests dfa-invalid-utf8 (a.b against a + overlong sequence + b) and
hangul-syllable (grep -a -v '^.*$' must keep every malformed line).

Reproduction

$ printf 'ok\nz\377z\n' > bad

Rust (incorrect)

$ LC_ALL=C.UTF-8 ./target/release/grep -a -c 'z.z' bad
1
# Exit code: 0

$ LC_ALL=C.UTF-8 ./target/release/grep -a -c -E 'z\Sz' bad
1
# Exit code: 0

$ LC_ALL=C.UTF-8 ./target/release/grep -a -v -x '.*' bad
# Output: (none)
# Exit code: 1

GNU (correct)

$ LC_ALL=C.UTF-8 /usr/bin/grep -a -c 'z.z' bad
0
# Exit code: 1

$ LC_ALL=C.UTF-8 /usr/bin/grep -a -c -E 'z\Sz' bad
0
# Exit code: 1

$ LC_ALL=C.UTF-8 /usr/bin/grep -a -v -x '.*' bad | od -c
0000000   z 377   z  \n
# Exit code: 0

Root cause

Every pattern is compiled with Oniguruma's UTF-8 encoding for both the pattern and the
subject (src/matcher.rs:405-408), and the subject line is handed to onig_search as raw
bytes without any validation (src/matcher.rs:439-450). Oniguruma's UTF-8 . and \S treat
any byte, valid or not, as the start of a character:

let mut compile_info = onig_sys::OnigCompileInfo {
    num_of_elements: 5,
    pattern_enc: &raw mut OnigEncodingUTF8,
    target_enc: &raw mut OnigEncodingUTF8,

Test

#[test]
fn dot_does_not_match_invalid_utf8_byte() {
    // 0xFE never appears in UTF-8, so it is not a character.
    let (_s, mut c) = ucmd();
    c.env("LC_ALL", "C.UTF-8")
        .args(&["-a", "-c", "q.q"])
        .pipe_in(&b"q\xfeq\nqrq\n"[..])
        .succeeds()
        .stdout_only("1\n");

    let (_s, mut c) = ucmd();
    c.env("LC_ALL", "C.UTF-8")
        .args(&["-a", "-c", "-E", "q\\Sq"])
        .pipe_in(&b"q\xfeq\n"[..])
        .fails_with_code(1)
        .stdout_only("0\n");
}

Exposed by the GNU grep test suite: dfa-invalid-utf8, hangul-syllable.

Activity

  1. added
    bug
    gnu-compat
    good first issue
    on Nov 15, 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

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions