grep - regex syntax errors print Oniguruma's wording instead of GNU's messages
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/matcher.rs 中的 CompiledPattern::compile 及其 compile_with 回退开始,然后检查现有的正则表达式错误转换逻辑和附近的测试。为 issue 中列出的 GNU 措辞情况添加覆盖,并运行针对这些参数的 grep 测试。完成标准是:列出的语法错误会产生与 GNU 兼容的 stderr,同时保留退出码 2,且 stdout 为空。
由索引模型根据 Issue 内容生成。
描述
Known: #151 (open PR) — https://github.com/uutils/grep/pull/151; #71 (merged PR) — https://github.com/uutils/grep/pull/71
When a basic or extended pattern has a syntax error, both tools print nothing on stdout and
exit 2, but the message differs. GNU grep uses a small fixed set of messages
(Unmatched [, [^, [:, [., or [=, Unmatched ( or \(, Trailing backslash, ...).
uu_grep prints invalid pattern "<pattern>": <Oniguruma message>. Exit codes and match
results are fine; only stderr differs, which breaks scripts and test suites that compare it.
Reproduction
Rust (incorrect)
$ printf 'a\n' | ./target/release/grep '[a'
grep: invalid pattern "[a": premature end of char-class
# Exit code: 2
$ printf 'a\n' | ./target/release/grep '\(a'
grep: invalid pattern "\(a": end pattern with unmatched parenthesis
# Exit code: 2
$ printf 'a\n' | ./target/release/grep 'a\'
grep: invalid pattern "a\": end pattern at escape
# Exit code: 2
GNU (correct)
$ printf 'a\n' | LC_ALL=C /usr/bin/grep '[a'
grep: Unmatched [, [^, [:, [., or [=
# Exit code: 2
$ printf 'a\n' | LC_ALL=C /usr/bin/grep '\(a'
grep: Unmatched ( or \(
# Exit code: 2
$ printf 'a\n' | LC_ALL=C /usr/bin/grep 'a\'
grep: Trailing backslash
# Exit code: 2
Observed mapping (Oniguruma code -> GNU 3.12 message, LC_ALL=C):
| Oniguruma error | example | GNU message |
|---|---|---|
ONIGERR_PREMATURE_END_OF_CHAR_CLASS |
[a, [[:alpha:] |
Unmatched [, [^, [:, [., or [= (a lone trailing [ as in a[ gives Invalid regular expression) |
ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS |
\(a, -E '(a' |
Unmatched ( or \( |
ONIGERR_UNMATCHED_CLOSE_PARENTHESIS |
a\) |
Unmatched ) or \) |
ONIGERR_END_PATTERN_AT_ESCAPE |
a\, -E 'a\' |
Trailing backslash |
ONIGERR_INVALID_REPEAT_RANGE_PATTERN |
a\{1 / a\{x\} |
Unmatched \{ / Invalid content of \{\} |
ONIGERR_INVALID_BACKREF |
\(a\)\2, -E '(a)\2' |
Invalid back reference |
Root cause
src/matcher.rs:333, the fallback arm in compile_with inside CompiledPattern::compile:
let message = match err.code {
ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS => "Invalid range end".to_string(),
_ => format!("invalid pattern \"{pattern}\": {}", err.message),
};
Only the reversed-range code is translated (added in #71); everything else falls through to
Oniguruma's text.
Test
#[test]
fn regex_syntax_errors_use_gnu_messages() {
let cases: [(&[&str], &str); 5] = [
(&["[x"], "Unmatched [, [^, [:, [., or [="),
(&["\\(x"], "Unmatched ( or \\("),
(&["-E", "(x"], "Unmatched ( or \\("),
(&["x\\)"], "Unmatched ) or \\)"),
(&["x\\"], "Trailing backslash"),
];
for (args, message) in cases {
let (_s, mut c) = ucmd();
c.args(args)
.pipe_in("x\n")
.fails_with_code(2)
.no_stdout()
.stderr_is(format!("grep: {message}\n"));
}
}
Reported by Anthropic's OSS Scanner (ANT-2026-PS745G19).
- 主要语言
- Rust
- 星标
- 147
- 派生
- 34
- 平均合并
- 3 天 18 小时
- 30 天内合并 PR
- 21
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
uutils/grep 的其他 Issue
-
grep - `-L` with `-m 0` or `-v ''` prints nothing; GNU still lists every file可能已有人在做 @yunusarda-sudo 于 5 天前认领。 未关闭bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 2 天内回复
-
grep - `-v -o` with `-A`/`-B`/`-C` prints nothing; GNU prints the matches found in the context lines未关闭bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 2 天内回复
-
grep - interval counts above 32767 are accepted; GNU rejects them with `Regular expression too big`可能已有人在做 @yunusarda-sudo 于 5 天前认领。 未关闭bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 2 天内回复
-
grep - `--exclude-dir=./dir` does not exclude the operand `./dir`可能已有人在做 @yunusarda-sudo 于 5 天前认领。 未关闭bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 2 天内回复
-
bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复
相似的 Issue
-
marketing
难度 2/5 半天 新手友好度 62/100
维护者通常 1 天内回复
-
S: Untriaged T: Bug
难度 2/5 1-3 小时 新手友好度 72/100
libnyanpasu/clash-nyanpasu#6537 ·
维护者通常 1 天内回复
-
难度 2/5 1 小时以内 新手友好度 62/100
tauri-apps/tao#1445 ·
维护者通常 1 天内回复
-
bug P3
难度 1/5 1-3 小时 新手友好度 88/100
-
go good first issue test
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 2 天内回复