grep - regex syntax errors print Oniguruma's wording instead of GNU's messages

Đang mở Phù hợp với người mới
#185 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend, cli

Hướng nghiên cứu

Bắt đầu tại src/matcher.rs, ở CompiledPattern::compile và phương án dự phòng compile_with, sau đó kiểm tra phần dịch lỗi regex hiện có và các bài kiểm thử lân cận. Bổ sung kiểm thử cho các trường hợp cách diễn đạt của GNU được liệt kê trong issue và chạy các bài kiểm thử grep kiểm tra những đối số này. Công việc hoàn tất khi các lỗi cú pháp được liệt kê tạo ra stderr tương thích với GNU, vẫn giữ mã thoát 2 và stdout rỗng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

gnu-compat good first issue oss-scanner

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).

Ngôn ngữ chính
Rust
Star
147
Fork
34
Merge trung bình
3 ngày 18 giờ
Pull request đã merge (30 ngày)
21

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của uutils/grep

Tất cả issue của uutils/grep

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.