Skip to content

grep - interval counts above 32767 are accepted; GNU rejects them with Regular expression too big #208

Open

Description

@sylvestre

GNU grep accepts interval counts up to 32767 and rejects anything larger, in BRE and ERE and
for either bound, with Regular expression too big and exit 2 (observed with GNU grep 3.12:
k{32767} compiles, k{32768} does not). uu_grep passes the interval to Oniguruma, whose
own limit is 100000 (ONIG_MAX_REPEAT_NUM), so k{40000} and k{1,40000} are searched
and exit 0/1.

Exposed by the GNU test bre (Spencer BRE test #44, a\{32768\} must exit 2).

Reproduction

Rust (incorrect)

$ printf 'kk\n' | ./target/release/grep -E 'k{40000}'
# Output: (none)
# Exit code: 1

$ printf 'kk\n' | ./target/release/grep 'k\{40000\}'
# Output: (none)
# Exit code: 1

$ printf 'kk\n' | ./target/release/grep -E 'k{1,40000}'
kk
# Exit code: 0

GNU (correct)

$ printf 'kk\n' | LC_ALL=C /usr/bin/grep -E 'k{40000}'
/usr/bin/grep: Regular expression too big
# Exit code: 2

$ printf 'kk\n' | LC_ALL=C /usr/bin/grep 'k\{40000\}'
/usr/bin/grep: Regular expression too big
# Exit code: 2

$ printf 'kk\n' | LC_ALL=C /usr/bin/grep -E 'k{1,40000}'
/usr/bin/grep: Regular expression too big
# Exit code: 2

Test

#[test]
fn interval_count_limit() {
    for args in [
        &["-E", "w{32800}"][..],
        &["w\\{2,50000\\}"][..],
        &["-E", "w{,32768}"][..],
    ] {
        let (_s, mut c) = ucmd();
        c.args(args)
            .pipe_in("ww\n")
            .fails_with_code(2)
            .no_stdout()
            .stderr_contains("Regular expression too big");
    }
    // The largest accepted count still works.
    let (_s, mut c) = ucmd();
    c.args(&["-E", "-c", "w{32767}"])
        .pipe_in("ww\n")
        .fails_with_code(1)
        .stdout_only("0\n");
}

Exposed by the GNU grep test suite: bre.

Activity

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