Repository navigation
Description
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' > badRust (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: 1GNU (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: 0Root 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