Skip to content

grep - a pattern that hits Oniguruma's retry limit panics and aborts (exit 134) #191

Open

Description

@sylvestre

A pattern with nested repetition such as ((x+)*)+$ makes Oniguruma give up with
ONIGERR_RETRY_LIMIT_IN_MATCH_OVER on a long enough non-matching line. uu_grep turns that
error into a panic!, so grep aborts with SIGABRT (exit 134) and a Rust backtrace hint.
GNU grep with -P reports exceeded PCRE's backtracking limit for that file and exits 2;
with -E GNU simply finds the match.

Exposed by the GNU tests pcre-abort (expects exit 2 and no output) and filename-lineno.pl
(test backtracking-with-file expects exit 2 and a FILE: exceeded PCRE's backtracking limit
diagnostic).

Reproduction

$ printf 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy\n' > bt

Rust (incorrect)

$ ./target/release/grep -P '((x+)*)+$' bt

thread 'main' panicked at src/matcher.rs:509:9:
Onig: Regex match error: retry-limit-in-match over
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
# Exit code: 134

$ ./target/release/grep -E '((x+)*)+$' bt
(same panic)
# Exit code: 134

GNU (correct)

$ LC_ALL=C /usr/bin/grep -P '((x+)*)+$' bt
/usr/bin/grep: bt: exceeded PCRE's backtracking limit
# Exit code: 2

$ LC_ALL=C /usr/bin/grep -E '((x+)*)+$' bt
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy
# Exit code: 0

Root cause

src/matcher.rs:503-514: every negative Oniguruma return value other than ONIG_MISMATCH
panics. search_leftmost, longest_end_at and is_match all go through it, and none of them
can report an error to the searcher:

fn onig_match_result(result: i32) -> Option<usize> {
    if result >= 0 {
        Some(result as usize)
    } else if result == onig_sys::ONIG_MISMATCH {
        None
    } else {
        panic!(
            "Onig: Regex match error: {}",
            onig_error_message(result, null())
        );
    }
}

Test

#[test]
fn backtracking_limit_is_an_error_not_a_panic() {
    let (_s, mut c) = ucmd();
    c.args(&["-P", "((m+)*)+$"])
        .pipe_in(format!("{}n\n", "m".repeat(45)))
        .fails_with_code(2)
        .no_stdout()
        .stderr_does_not_contain("panicked");
}

Exposed by the GNU grep test suite: pcre-abort, filename-lineno.pl.

Activity

  1. added
    bug
    gnu-compat
    crash
    on Nov 10, 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

    bugcrashgnu-compat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions