Skip to content

sed: treat v as a no-op after a successful version check - #671

Open
emirmrkaljevic wants to merge 2 commits into
uutils:mainfrom
emirmrkaljevic:sed-v-command-noop
Open

emirmrkaljevic wants to merge 2 commits into
uutils:mainfrom
emirmrkaljevic:sed-v-command-noop

Conversation

@emirmrkaljevic

Copy link
Copy Markdown
Contributor

Fixes #669.

The compiler already accepted the GNU v version assertion and rejected versions newer than 4.9, but a successful check still panicked at runtime with invalid command code as soon as there was input. Tests that ran sed v with no stdin never reached the processor, so they stayed green.

v is now a no-op at execution time. I also matched a few related GNU sed 4.9 details I hit while reproducing this with behave:

  • The version argument stops at ;, #, or }, so v;s/a/A/ runs the substitute.
  • The command accepts up to two addresses, like GNU.
  • The advertised version is the two-component 4.9, so v3.99 and v4.8.99 succeed, v4.9 succeeds, and v4.9.0 is newer and is rejected.

Added tests that pipe input for the success cases, plus tests for the semicolon, addresses, older components, and v4.9.0. The existing v999 test now expects expected newer version of sed instead of a parse error.

cargo test --test tests version, cargo fmt --check, and cargo clippy --all-targets --all-features -- -D warnings pass.

The compiler already accepted v, but the processor panicked with
invalid command code once there was input. Execution is now a no-op.
The version argument stops at ;, #, or }, addresses are accepted,
and the comparison follows GNU sed 4.9 (two components, so v4.9
succeeds and v4.9.0 does not).
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #671      +/-   ##
==========================================
+ Coverage   95.31%   95.41%   +0.10%     
==========================================
  Files          14       14              
  Lines        7512     7658     +146     
  Branches      443      457      +14     
==========================================
+ Hits         7160     7307     +147     
+ Misses        351      350       -1     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.88% <100.00%> (+0.07%) ⬆️
ubuntu_latest 96.81% <100.00%> (+0.07%) ⬆️
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 Nov 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 11 untouched benchmarks


Comparing emirmrkaljevic:sed-v-command-noop (e0607eb) with main (cab1c6e)

Open in CodSpeed

Comment thread src/sed/compiler.rs Outdated
_ => return compilation_error(lines, line, "invalid version of sed"),
let c = line.current();
// The version argument ends at a command separator; leave it in place.
if matches!(c, ';' | '#' | '}') {

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.

v4.9 ;p or 1{v4.9 } still fail with invalid version, GNU stops at blanks too.
could you please eat_spaces() and reuse parse_command_ending like the other commands instead of a custom separator check?

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.

Thanks, done. After the version I now call eat_spaces() and parse_command_ending, so v4.9 ;p and 1{v4.9 } work. A following # is left in place so the comment can be parsed.

Comment thread src/sed/compiler.rs Outdated
/// Return true if `requested` is newer than the implementation version.
/// Equal shared components with extra requested components count as newer,
/// matching GNU sed 4.9 (`v4.9` succeeds, `v4.9.0` does not).
fn version_is_newer_than(requested: &[u32], current: &[u32]) -> bool {

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.

slices already compare lexicographically, a longer one counting as greater, so components.as_slice() > &GNU_VERSION[..] should do it. do we need this function?

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.

You're right, the helper wasn't needed. I now compare with components.as_slice() > &GNU_VERSION[..].

Comment thread src/sed/compiler.rs
if c == '.' {
if current.is_empty() {
// Leading or consecutive dots are empty components (GNU: `v.9`).
components.push(0);

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.

the parse + push logic shows up 3 times, and push(0) twice. splitting the argument on '.' would be simpler, no?

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.

Agreed, that's simpler. I now collect the argument and split it on ..

Comment thread tests/by-util/test_sed.rs
}

#[test]
fn test_version_stops_at_semicolon() {

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 also add tests for } and # after the version (e.g. 1{v4.9}), a blank before ;, and a component that overflows u32 (codecov flags those error branches)

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 tests for 1{v4.9}, 1{v4.9 }, v4.9#c, a blank before ;, and a component that overflows u32.

Stop the version argument at whitespace or a command ending, split
it on '.', and compare components as slices against the advertised
4.9 version. Also cover '}', '#', a blank before ';', and a u32
overflow.
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.

sed: v command panics when the version check succeeds

2 participants