Skip to content

sed: point the invalid back-reference caret at the reference - #609

Merged
sylvestre merged 2 commits into
uutils:mainfrom
sylvestre:sed-backref-caret
Nov 8, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
sylvestre:sed-backref-caret

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

An invalid back-reference such as s/\(a\)/\2/ is only detected after the flags are parsed, so the UUTILS_DIAG caret landed past the end of the command. It now points at the \2, including when the replacement spans several lines.

$ UUTILS_DIAG=always sed 's/\(a\)/\2/'
sed: <script argument 1>:1:9: error: invalid reference \2 on `s' command's RHS
   ╭─[ <script argument 1>:1:9 ]
   │
 1 │ s/\(a\)/\2/
   │         ─
───╯

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused change preserves validation behavior and adds regression coverage, with no unresolved findings.

No open issues 🎉

What changed in this PR

Corrects sed’s invalid back-reference diagnostic so the caret points at the offending reference rather than past the command.

Changes:

  • Records the first occurrence of the highest-numbered replacement reference.
  • Uses that location for validation errors.
  • Adds a regression test for caret placement.
File Description
tests/​by-util/​test_sed.rs Tests caret placement with repeated references.
src/​sed/​compiler.rs Captures and uses the reference’s source location.
src/​sed/​command.rs Stores an optional location in replacement templates.

Review effort: Balanced

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.10%. Comparing base (273dcfb) to head (9a6bd7d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #609   +/-   ##
=======================================
  Coverage   95.10%   95.10%           
=======================================
  Files          14       14           
  Lines        7490     7499    +9     
  Branches      443      444    +1     
=======================================
+ Hits         7123     7132    +9     
  Misses        366      366           
  Partials        1        1           
Flag Coverage Δ
macos_latest 96.59% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 96.53% <100.00%> (+<0.01%) ⬆️
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.

@codspeed

codspeed Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 3.52%

1 warning

⚠️ 4 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 4 improved benchmarks
✅ 7 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ no_op_short 2.2 s 2.1 s +4.77%
⚡ access_log_translit 991.9 ms 956.9 ms +3.66%
⚡ access_log_no_op 1.6 s 1.5 s +3.3%
⚡ access_log_append 1.1 s 1 s +2.38%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing sylvestre:sed-backref-caret (9a6bd7d) with main (273dcfb)

Open in CodSpeed

An invalid back-reference such as `s/\(a\)/\2/` is only detected once
the flags have been read, so the diagnostic caret landed past the end of
the command. Record where the highest `\N` appears in the replacement
and report the error there.
compile_replacement records a location for every \N it reads, so the
location is only absent when no group is referenced and the check cannot
fire. Bind it in the condition instead of keeping an untested fallback,
and cover a replacement continued on a second line.
Copilot AI balanced review requested due to automatic review settings November 7, 2026 14:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit 5e2821e into uutils:main Nov 8, 2026
31 checks passed
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.

2 participants