Skip to content

ERE quantifiers are not validated: {m}, {m,} and {m,n} #289

Closed

Description

@e-kwsm

revision: 3504109

https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap09.html#tag_09_04_06

When an ERE matching a single character or an ERE enclosed in parentheses is followed by an interval expression of the format "{m}", "{m,}", or "{m,n}", together with that interval expression it shall match what repeated consecutive occurrences of the ERE would match. The values of m and n are decimal integers in the range 0<=m<=n<={RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression "{m}" matches exactly m occurrences of the preceding ERE, "{m,}" matches at least m occurrences, and "{m,n}" matches any number of occurrences between m and n, inclusive.

But uutils does not check the condition—m>n:

$ gnused -n -E -e '/x{2,1}/p' <<< x
sed: -e expression #1, char 8: Invalid content of \{\}
$ echo $?
1
$ bsdsed -n -E -e '/x{2,1}/p' <<< x
bsdsed: 1: "/x{2,1}/p
": RE error: Invalid content of \{\}
$ echo $?
1
$ cargo run --quiet -- -n -E -e '/x{2,1}/p' <<< x
x
$ echo $?
0

and negative m, e.g. x{-1} and x{-1,}.

Activity

  1. e-kwsm commented on Feb 5, 2026

    @e-kwsm
    ContributorAuthor

    uutils does not check expression in {}: empty, nonintegral,

    $ gnused -n -E -e '/.{x}/p' <<< x
    sed: -e expression #1, char 6: Invalid content of \{\}
    $ echo $?
    1
    $ bsdsed -n -E -e '/.{x}/p' <<< x
    bsdsed: 1: "/.{x}/p
    ": RE error: Invalid content of \{\}
    $ echo $?
    1
    $ cargo run --quiet -- -n -E -e '/.{x}/p' <<< x
    x
    $ echo $?
    0

    and leading and trailing whitespaces.

  2. LoukasPap commented on Feb 9, 2026

    @LoukasPap
    Contributor

    Hello! @e-kwsm Can I give it a try or are you working on it? From what I've understood, we must basically add validity checks for the ERE quantifiers.

  3. e-kwsm commented on Feb 12, 2026

    @e-kwsm
    ContributorAuthor

    Hi, no I'm not addressing this issue.

  4. LoukasPap commented on Feb 15, 2026

    @LoukasPap
    Contributor

    Hello again. I think I've noticed a wrong behavior of BRE.

    Like the ERE standard above, the same applies to BRE standard when using backslash \ in front of the { and }:

    https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap09.html#tag_09_03_08:~:text=ab%22%2E-,When,inclusive

    When a BRE matching a single character, a subexpression, or a back-reference is followed by an interval expression of the format "\{m\}", "\{m,\}", or "\{m,n\}", together with that interval expression it shall match what repeated consecutive occurrences of the BRE would match. The values of m and n are decimal integers in the range 0 <=m<=n<={RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression "\{m\}" shall match exactly m occurrences of the preceding BRE, "\{m,\}" shall match at least m occurrences, and "\{m,n\}" shall match any number of occurrences between m and n, inclusive.'

    This can be seen in the sed docs too: https://www.gnu.org/software/sed/manual/sed.html#BRE-syntax

    Example outputs:

    # captures nothing
    $ cargo run --quiet -- -n -e '/x\{1\}/p' <<< x
    
    # captures x
    $ sed -n -e '/x\{1\}/p' <<< x
    x
  5. e-kwsm commented on Feb 15, 2026

    @e-kwsm
    ContributorAuthor

    Yes, sed must support \{m\}, \{m,\} and \{m,n\} BRE quantifiers (#288 (comment)).

  6. LoukasPap commented on Feb 23, 2026

    @LoukasPap
    Contributor

    @e-kwsm While matching test cases' outputs of sed and uutils/sed, I found that case:

    Running a sed command like this returns everything:

    $ sed --quiet -E -n '/v{,}/p' <<< $'xxx\nddd\naaa'
    xxx
    ddd
    aaa

    From my experiments, x{,} is treated like x* (zero or more), thus capturing everything.

    This can be partially testified in the below example. It must capture a character d followed by 0 or more v's. Only line 2 matches the pattern.

    $ sed --quiet -E -n '/dv{,}/p' <<< $'xxx\nddd\naaa'
    ddd

    The problem is, the regex crate does not accept v{,}:

    $ cargo run --quiet -- -E -n '/v{,}/p' <<< $'xxx\nddd\naaa'
    sed: <script argument 1>:1:7: error: invalid regex 'v{,}': regex parse error:
        v{,}
          ^
    error: repetition quantifier expects a valid decimal

    Solution

    We can overcome that by transforming {,} to asterisk * when parsing it (of course this is needed only for ERE - in case of BRE, the v{,} is parsed as a literal string). My only hesitation for that is that we alter the input pattern, but I can't find another way around.

  7. e-kwsm commented on Feb 23, 2026

    @e-kwsm
    ContributorAuthor

    Yeah, GNU and BSD sed’s seem to treat {,} as {0,} viz *.

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

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions