Skip to content

sed: reject an ERE quantifier with nothing to repeat - #616

Merged
sylvestre merged 3 commits into
uutils:mainfrom
qodelicious:ere-invalid-preceding-regex
Oct 25, 2026
Merged

sylvestre merged 3 commits into
uutils:mainfrom
qodelicious:ere-invalid-preceding-regex

Conversation

@qodelicious

Copy link
Copy Markdown
Contributor

In an ERE a quantifier needs something before it to repeat. GNU sed rejects *, +, ? and {m,n} at the start of the regex or right after (, |, ^ or $ with "Invalid preceding regular expression".

On main most of these already failed, but with a raw regex crate error (invalid regex '*': regex parse error ... repetition operator missing expression), and ^*, $* and a^* were accepted and matched. Now the regex parser tracks whether the last token can be repeated and reports GNU's message. Like GNU it reports once the whole regex has been read, so the column matches GNU's char number for addresses (/*/p gives 1:3, /a|*b/p gives 1:6, /^*/p gives 1:4). BRE is unchanged since a leading * is literal there.

Escaped characters, bracket expressions, groups and other quantifiers still count as something to repeat, so a**, a+?, ()*, (a|)*, [a]*, a{1}*, \(* and \** work as before. I checked every case in the tests against GNU sed 4.9.

Added test_ere_quantifier_without_preceding_atom and test_ere_quantifier_after_atom. The first fails on main. cargo test, cargo fmt --check and cargo clippy --all-targets --all-features -D warnings pass.

Fixes #290

In an ERE, *, +, ? and {m,n} at the start of the regex or right after
(, |, ^ or $ now fail with GNU's "Invalid preceding regular expression"
error. ^* and $* used to be accepted, and the other cases gave a raw
regex crate error.

Fixes uutils#290
@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.07%. Comparing base (caef481) to head (d0e59da).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #616      +/-   ##
==========================================
+ Coverage   94.93%   95.07%   +0.13%     
==========================================
  Files          14       14              
  Lines        7292     7436     +144     
  Branches      434      439       +5     
==========================================
+ Hits         6923     7070     +147     
+ Misses        368      365       -3     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.58% <100.00%> (+0.11%) ⬆️
ubuntu_latest 96.51% <100.00%> (+0.11%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/by-util/test_sed.rs
("/^*/p", 4),
("/$*/p", 4),
("/a^+/p", 5),
] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a s case too, e.g. s/*/x/, it goes through the same parser

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added s//x/, s/a|+b/x/ and s/^/x/g. They all get the same error now.

@sylvestre
sylvestre merged commit 301a2b7 into uutils:main Oct 25, 2026
31 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

quantifier following NO atom

2 participants