Skip to content

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

Open
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:sed-backref-caret
Open

sylvestre wants to merge 1 commit 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/
   │         ─
───╯

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.

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

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.84%. Comparing base (b1ea0dc) to head (6ac57c8).

Files with missing lines Patch % Lines
src/sed/compiler.rs 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #609      +/-   ##
==========================================
- Coverage   94.85%   94.84%   -0.01%     
==========================================
  Files          14       14              
  Lines        7230     7236       +6     
  Branches      432      432              
==========================================
+ Hits         6858     6863       +5     
- Misses        371      372       +1     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.38% <93.33%> (-0.02%) ⬇️
ubuntu_latest 96.32% <93.33%> (-0.02%) ⬇️
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 7.69%

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚡ 2 improved benchmarks
✅ 9 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ number_fix 1.3 s 1.2 s +12.01%
⚡ access_log_subst 2.2 s 2.1 s +3.55%

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 (6ac57c8) with main (b1ea0dc)

Open in CodSpeed

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